Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple JQuery .getJSON callbacks do not fire

Tags:

json

jquery

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?

like image 432
Ian Avatar asked Mar 05 '26 10:03

Ian


1 Answers

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.

like image 88
Beetroot-Beetroot Avatar answered Mar 06 '26 23:03

Beetroot-Beetroot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!