Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross domain ajax json

I'm on site.com 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.com:8080/json/1. Origin http://site.com is not allowed by Access-Control-Allow-Origin.

my code:

    $.get('http://site.com: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.com:8080/' + this.id
    }
});

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

Thanks.

like image 697
FriiSource Avatar asked Feb 14 '11 00:02

FriiSource


1 Answers

If 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.

like image 68
jmort253 Avatar answered Oct 12 '22 10:10

jmort253