Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对nodejs服务器的Javascript xmlhttp get请求不返回任何内容_Javascript_Node.js_Xmlhttprequest - Fatal编程技术网

对nodejs服务器的Javascript xmlhttp get请求不返回任何内容

对nodejs服务器的Javascript xmlhttp get请求不返回任何内容,javascript,node.js,xmlhttprequest,Javascript,Node.js,Xmlhttprequest,在我开始之前,我想告诉大家,我自己做了很多努力来寻找这个问题的解决方案 这是我的nodejs服务器: var http = require('http'); http.createServer(function (req, res) { console.log("Recived request url " + req.url); var sname = req.url.search("name"); if(sname != -1){ sname = sna

在我开始之前,我想告诉大家,我自己做了很多努力来寻找这个问题的解决方案

这是我的nodejs服务器:

var http = require('http');
http.createServer(function (req, res) {
    console.log("Recived request url " + req.url);
    var sname = req.url.search("name");
    if(sname != -1){
        sname = sname + "name".length + 1;
        var from = sname;
        while(req.url[sname] != '?' && sname<req.url.length){
            sname++;
        }
        var name = req.url.substring(from,sname);
        console.log("Returning parameter of name - " + name);
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end(name+'\n');
    }
    else{
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end('Error - ask about name\n');
    }

}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
返回的xmlHttp.responseText为空。 为什么?这是我的问题。

这就是我的nodejs服务器在这件事上要说的

Recived request url /?name=Mateusz
Returning parameter of name - Mateusz
Recived request url /?

如果我curl我的服务器,它将返回mi正确的值。

xmlHttp.responseText
在您返回它时尚未填充。Ajax是异步的


查看并绑定HTTP响应到达时要侦听的事件处理程序,并处理其中的数据。

无法接受答案。看来你是对的。我给返回添加了一个超时,得到了一个未定义的返回,但最后得到了一些东西。我将尝试将事件绑定到处理程序,或者尝试获取状态更改的反应。现在我有事情要做。非常感谢。作为一个技巧:看一看
url.parse
,这是一种解析url和查询字符串的非常简单的方法(
var query=require('url')。parse(req.url,true)。query;var sname=query.name;
)另一个技巧:直接使用XMLHttpRequest对象是一种痛苦,在我看来,它没有增加任何值得理解的地方。我建议使用jQuery或类似工具来避免编写自己的http客户端代码时出现错误。
Recived request url /?name=Mateusz
Returning parameter of name - Mateusz
Recived request url /?