I am currently struggling with $.getJSON()
and resulting .fail()
function. I am trying to get it so that when a $.getJSON()
fails, it changes the variables value to something else - such as:
var mangoes = $.getJSON("mangoesurl.com/json").fail(mangoes="{value:0}");
I cannot use another variable as I am also using $.when()
later to retrieve that data from the mangoes JSON.
Does anybody know if this is possible, how I can do it or an alternative? Any help is greatly appreciated, thanks.
var mangoes = $.getJSON("mangoesurl.com/json").fail(mangoes="{value:0}");
var mangoes2 = $.getJSON("mangoesurl.com/json2").fail(mangoes="{value:0}");
$.when(mangoes, mangoes2).then(data1, data2){
}
If I understand correctly, this is what you need:
var mangoes = $.getJSON("mangoesurl.com/json").fail(function(){
return {value:0};
});
In this way mangoes is empty after the response. Basically this code gets the JSON but does nothing with it. To put the JSON result into mangoes you need this:
var mangoes = $.getJSON("mangoesurl.com/json")
.success(function(response){
return response;
});
.fail(function(){
return {value:0};
});
With the When sintax you can't assign content to mangoes...you have to change the logic
var mangoes, mangoes2;
var deferred1 = $.getJSON("mangoesurl.com/json");
var deferred2 = $.getJSON("mangoesurl.com/json2");
$.when(deferred1, deferred2).then(function(data1, data2){
mangoes = data1;
mangoes2 = data2;
}, function(error){
mangoes || mangoes = {value:0};
mangoes2 || mangoes2 = {value:0};
});