问题描述
我已经设置了NodeJS,当我浏览到URL时它正在返回数据: http://184.106.206.235
I've set up NodeJS and it's returning data when I browse to the URL: http://184.106.206.235
但是,当我尝试使用$.getJSON
调用该URL时,回调显示null
表示data
变量,而"success"
表示textStatus
变量.
However, when I try to call that URL using $.getJSON
, the callback shows null
for the data
variable and "success"
for the textStatus
variable.
我想这可能是跨域的,但是令我惊讶的是textStatus
会说"success"
.
I imagine this could be a cross-domain thing, but I'm surprised that the textStatus
says "success"
if that's the case.
如果有帮助,请参见服务器端JS:
In case it's helpful, here's the server-side JS:
http.createServer(function(req, res){
var output = {message: "Hello World!"};
var body = JSON.stringify(output);
res.writeHead(200, {'Content-Type': 'application/json', 'Content-Length': body.length});
res.end(body);
}).listen(80, "184.106.206.235");
有什么想法吗?
推荐答案
将"Access-Control-Allow-Origin":"*"属性添加到您的writeHead()调用中:
Add the "Access-Control-Allow-Origin": "*" property to your writeHead() call:
res.writeHead(200, {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*"
});
这篇关于NodeJS不会将数据返回到jQuery.getJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!