weixin_33749242 2015-09-18 22:17 采纳率: 0%
浏览 48

Ajax get和NodeJS?

I am trying to display my text file content on my webpage when a button is clicked. I am using AJAX to do this and am not able to figure out what's wrong with my code.

main.js

var http = require('http'),
    fs = require('fs'),
    process = require('child_process');

function send404(res) {
    res.writeHead(404);
    res.write('Error');
    res.end()
}

var value;
var server = http.createServer(function (req, res) {

    if (req.method == 'GET' && req.url == '/') {

        var path = 'java/hello.java'; 

        res.writeHead(200, {'content-type':'text/html'});

        fs.readFile('ajax.html', 'utf8', function (err, data) {

            if (err) {
                return console.log(err);
            } else {
                res.writeHeader(200, {"Content-Type": "text/html"});
                res.write(data);
            }
        });
    }

}).listen(3000);

Here is my HTML file code ajax.html

<!DOCTYPE html>
<html>
<head>
<script>
    function loadXMLDoc() {
        var xmlhttp;
        if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        } else { // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "aa.txt", true);
        xmlhttp.send();
    }
</script>
</head>
<body>
    <div id="myDiv"><h2>Let AJAX change this text</h2></div>
    <button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • Didn"t forge 2015-09-18 22:48
    关注

    I'm going to base my answer on what I think you're trying to do: load some text from a file using AJAX in a browser and display it in a div.

    My first observation is: you don't need anything to run server-side to make this happen. The purpose of Nodejs is to create server-side applications using Javascript. Therefore: you don't need Nodejs.

    My second observation is that you have tagged your question with JQuery...but you haven't used it. One feature of JQuery is that it wraps up all that XMLHttpRequest stuff and make it nice to use and compatible across all (or nearly all) browsers. Therefore: you should use JQuery.

    The following is a very simple example of using this to do what you want to do:

    <div id="myDiv">Hello</div>
    <button type="button" id="myButton">Change Content</button>
    $(document).ready(function() {
      $("#myButton").on("click", function() {
        $("#myDiv").load("aa.txt");
      });
    });
    

    This example uses JQuery, so you will need to link to the JQuery library in your HTML page.

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能