user874565
user874565

Reputation: 9

How to use $.getJSON() properly?

I'm having really strange problem with $.getJSON() on localhost. My test code is below:

$(document).ready(function(){
        var url = "http://script.mydomain.com/location/newid"
        var url2 = "http://localhost/cms/location/newid"

          $.getJSON(url2, function(id) {
                alert(id);
          });
});

For url2 it works perfect but for url it's not working. Could anyone give some hint to solve this problem? These urls returns json (example: "34") if anyone would like to know.

EDIT [SOLVED]:

If anyone in future will use $.getJSON remember that you can't call "alien" domains.

When I executed overhead script under script.mydomain.com domain everything works fine!

Upvotes: 0

Views: 1673

Answers (3)

defuz
defuz

Reputation: 27601

Chances are you have a problem that you are trying to run in a browser ajax-request from the local host, to the domain cw.uppercut.pl.

This is not an issue of the library.

Upvotes: 1

sje397
sje397

Reputation: 41812

You are limited by browser security to obtaining code from your own website (jquery uses XMLHttpRequest for getJSON). To get around it, you need to use jsonp (jquery adds a script tag) or create and append a script tag to your document.

Upvotes: 1

Exelian
Exelian

Reputation: 5888

AJAX request are limited by a cross-domain policy. Basically, you can't do ajax requests if they're not going to the server the original site was hosted on. It's slightly more complicated then that, but I would assume this to be the case.

Upvotes: 6

Related Questions