使用XmlHttpRequest,responseText为了获得节点js响应不起作用

时间:2018-10-20 15:02:20

标签: javascript html node.js ajax

我正在尝试使用节点js和ajax构建客户端和服务器...我正在尝试从节点js服务器获取信息,但我不明白... xmlhttp.responseText不返回任何内容。

站点(客户端):

<head>
    <title>Testing...</title>
    <script>
        function loadXMLDoc()
        {
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            //xmlhttp.addEventListener("load", server_test); <-- not working as well...
            xmlhttp.onreadystatechange = server_test;
            xmlhttp.open("GET","http://localhost:8001/?web=www.google.com", true);
            xmlhttp.send();
        }


        function server_test()
        {
            document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
    </script>
</head>
<body>
    <div id="myDiv"><h2>Waiting for response...</h2></div>
    <button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>

服务器端:

var http = require('http'),
url = require('url');

http.createServer(function(request, response){

console.log("request recieved");
var web = url.parse(request.url, true).query.web;
console.log("Sending info about " + web);

response.writeHead(200, {"Content-Type": "text/plain"});
response.write(web)
response.end(web);

console.log("Info sent...");


}).listen(8001);
console.log("server initialized");

我搜索了几个小时,但仍然找不到答案,希望可以在这里找到它。谢谢您的帮助。

0 个答案:

没有答案