nodejs jsonp - 访问控制允许原因错误 [英] nodejs jsonp - Access-Control-Allow-Origin Error
问题描述
我确定JSONP意味着没有跨域限制。我试图做一个JSONP服务使用节点和快递。这里是简单的代码:
I'm pretty sure JSONP means no cross domain restrictions. I'm trying to make a JSONP service using node and express. Here is simple code:
self.routes['/portfolio'] = function(req, res) {
// Website you wish to allow to connect
res.jsonp({ "my": "object" });
};
当我这样做时:
$.getJSON('http://127.0.0.1:8080/portfolio', function(data){ console.log(data)});
我收到此错误。
XMLHttpRequest无法加载 http: //127.0.0.1:8080/portfolio 。
'Access-Control-Allow-Origin'标头的值为
' http:// 127.0.0.1:8080/portfolio '不等于提供的
起源。原因 http://api.jquery.com 因此不允许
访问。
XMLHttpRequest cannot load http://127.0.0.1:8080/portfolio. The 'Access-Control-Allow-Origin' header has a value 'http://127.0.0.1:8080/portfolio' that is not equal to the supplied origin. Origin 'http://api.jquery.com' is therefore not allowed access.
有人可以解释这里发生了什么吗?为什么我们需要提供标头值,如果这是JSONP。如何使这项工作跨域?
Can someone explain what is going on here? Why do we need to supply header value if this is JSONP. How can I make this work cross domain?
推荐答案
$。getJSON
不会将请求视为JSONP
$.getJSON
does not treat the request as JSONP unless the request URL includes a string like callback=?
.
因此,请尝试这样做:
So, try doing this instead;
$.getJSON('http://127.0.0.1:8080/portfolio?callback=?', function(data){ console.log(data)});
这篇关于nodejs jsonp - 访问控制允许原因错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!