Side navigation
#6327 closed bug (invalid)
Opened March 21, 2010 01:12AM UTC
Closed March 21, 2010 01:41AM UTC
.getJSON callback fires before request returns
Reported by: | flying_fingersyka | Owned by: | |
---|---|---|---|
Priority: | Milestone: | 1.4.3 | |
Component: | unfiled | Version: | 1.4.2 |
Keywords: | .getJSON callback | Cc: | |
Blocked by: | Blocking: |
Description
$.getJSON (and $.ajax with dataType:'json') seem to fail for some reason when getting a json object from Twitter. For example:
$.getJSON('http://search.twitter.com/trends.json', function(json, textStatus) {
alert(textStatus);
alert(json);
});
In the upper case, the textStatus is alerted as "SUCCESS", but the json object is alerted as "null".
The Twitter json objects are valid. In fact when I download the very same trends.json file and put it in a local directory $.getJSON works fine:
$.getJSON('http://127.0.0.1/sample/trends.json', function(json, textStatus) {
alert(textStatus);
alert(json);
});
In this case the textStatus is "SUCCESS" and the json object is alerted as [object Object]...and naturally all further manipulations with the json object work.
I also tried getting the same trends.json file from my own web domain and it FAILED again. Basically it fails for any URL apart from the local one. Thus, my logic tells me that, since getting the json file from external server takes a bit more time, the callback function fires before that data is received. That would explain why it works on a local server when respond time is faster...
I would very much appreciate some thoughts on the matter and possibly a workaround..Let me know if you need any more details
Thanks a bunch!!!
$.getJSON is just a wrapper around $.ajax for a simple case. You can't make a cross-domain request with xmlhttprequest, only jsonp. The Twitter API delivers plain text/json file and it appears from their docs that you can't use the standard callback=? notation to turn it into jsonp, where the requested page would call your callback with the json.
This isn't a bug in jQuery so it would be best to continue the discussion on http://forum.jquery.com if needed.