【发布时间】:2012-08-08 23:33:33
【问题描述】:
我正在尝试从网络上的 javascript 代码到 Node.js 中的服务器进行简单的连接(请求 - 响应)。
我已尝试以follows 提出请求:
var request = new XMLHttpRequest();
request.open('GET', 'http://localhost:4444/', false);
request.send();
if (request.status === 200) {
console.log(request.responseText);
}
运行这段代码,FireBug 出现错误
我继续搜索,我发现这个方法只是在同一个域上发出 GET 请求。要发出跨域请求,我们必须使用其他策略。
我找到了jQuery method,看来我走对了:
$.get(
'http://localhost:4444/',
function(data) {
alert("sucess");
//Do anything with "data"
}
);
在这种情况下,我得到了相同的回复 without the error。
它似乎有效,但从未显示“警报”消息!发生什么了?我做错了什么?
Node.js 服务器代码是:
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/html"});
response.write("Response");
response.end();
}).listen(4444);
【问题讨论】:
标签: javascript jquery node.js get request