admin管理员组文章数量:1312824
I'm on site trying to grab some json data from my node.js server serving on port 8080.
I get this Error message:
XMLHttpRequest cannot load :8080/json/1. Origin is not allowed by Access-Control-Allow-Origin.
my code:
$.get(':8080/1/', {}, function (Data) {
console.log(Data);
}, "json");
But it is the same domain though! :(
Also consider my backbone.js model:
model = Backbone.Model.extend({
url: function() {
return ':8080/' + this.id
}
});
Is there any way to resolve this other than using jsonp?
Thanks.
I'm on site. trying to grab some json data from my node.js server serving on port 8080.
I get this Error message:
XMLHttpRequest cannot load http://site.:8080/json/1. Origin http://site. is not allowed by Access-Control-Allow-Origin.
my code:
$.get('http://site.:8080/1/', {}, function (Data) {
console.log(Data);
}, "json");
But it is the same domain though! :(
Also consider my backbone.js model:
model = Backbone.Model.extend({
url: function() {
return 'http://site.:8080/' + this.id
}
});
Is there any way to resolve this other than using jsonp?
Thanks.
Share Improve this question asked Feb 14, 2011 at 0:30 FriiSourceFriiSource 3212 gold badges6 silver badges12 bronze badges 2- 9 The ports must match as well, otherwise jsonp is indeed the only option. – Pekka Commented Feb 14, 2011 at 0:35
- I'm having the same issue. IMHO, this should be clarified in the documentation (that ports are considered to be part of the domain). Also, are requests across subdomains illegal as well? – snapfractalpop Commented Mar 30, 2012 at 14:59
3 Answers
Reset to default 7If you're making the call to the same domain, why do you have the absolute path in your $.get
request?
Try this:
$.get('/1/', {}, function (Data) {
console.log(Data);
}, "json");
model = Backbone.Model.extend({
url: function() {
return '/' + this.id
}
});
If you are truly making the call on the same domain, then the above code should work.
Alternatively, you could modify your node.js server to allow Cross-Origin Resource Sharing (CORS). This only works in modern browsers. The simplest (and least secure) thing to do would be for your node.js server to emit the header "Access-Control-Allow-Origin: http://site.". You can learn more about CORS here: http://www.w3/TR/cors/
You have to make the call to the same domain and port your script was loaded from. You should be able to use jmort's code.
本文标签: javascriptCross domain ajax jsonStack Overflow
版权声明:本文标题:javascript - Cross domain ajax json - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741887529a2403110.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论