Cross domain ajax json

I am on a site trying to grab some json data from my node.js server serving port 8080.

I get this error message:

XMLHttpRequest cannot load http://site.com:8080/json/1. Origin http://site.com is not allowed by Access-Control-Allow-Origin. 

my code is:

  $.get('http://site.com:8080/1/', {}, function (Data) { console.log(Data); }, "json"); 

But this is the same domain !: (

Also consider my backbone.js model:

 model = Backbone.Model.extend({ url: function() { return 'http://site.com:8080/' + this.id } }); 

Is there any way to resolve this other than using jsonp?

Thanks.

+6
javascript jquery ajax cross-domain
source share
3 answers

Alternatively, you can modify your node.js server to allow resource sharing (CORS). This only works in modern browsers. The simplest (and least secure) thing would be for your node.js server to emit the header "Access-Control-Allow-Origin: http://site.com ". You can learn more about CORS here: http://www.w3.org/TR/cors/

+2
source share

If you are calling the same domain, why do you have an absolute path in your $.get request?

Try the following:

 $.get('/1/', {}, function (Data) { console.log(Data); }, "json"); model = Backbone.Model.extend({ url: function() { return '/' + this.id } }); 

If you are actually making a call in the same domain, then the above code should work.

+7
source share

You must make a call to the same domain and from which your script was loaded. You should be able to use jmort code.

0
source share

All Articles