NodeJS不会将数据返回到jQuery.getJSON [英] NodeJS won't return data to jQuery.getJSON

查看:90
本文介绍了NodeJS不会将数据返回到jQuery.getJSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆