Ian
Ian

Reputation: 483

Multiple JQuery .getJSON callbacks do not fire

I am attempting to run the following code snippet:

var myVar1 = $.getJSON('myurl', function(json) {
    console.log("debug1", json);
});
var myVar2 = $.getJSON('myurl2', function(json2) {
    console.log("debug2", json2);
});

And I never see the "debug2" entry appear in my console log. When I check the status of myVar2 after the requests have completed, I see it populated with the correct data. When I append a .complete() statement at the end of the of the second .getJSON() request, the .complete() function will fire correctly.

Using jQuery 1.7.2 and lastest stable of google chrome. Why will the second callback function not fire?

Upvotes: 3

Views: 1052

Answers (1)

Beetroot-Beetroot
Beetroot-Beetroot

Reputation: 18078

Try :

var myVar2 = $.getJSON('myurl2').success(function(){
    console.log("debug2 - success");
}).error(function(){
    console.log("debug2 - error");
});

You will probably see the error message and not success. I would guess that 'myurl2' does not exist though it could be that it does exist but the script makes an HTTP response with an error heading. I think a JSON decode failure will also fire the error callback.

Upvotes: 4

Related Questions