Handling different success and fail states for multiple ajax call using deferred objects in jQuery -
$.when
returns deferred object multiple ajax calls queried simultaneously.
if succeeds .done()
executes , if 1 of url fails .fail()
executes.
how handle partial success states? (i.e) if 5 urls passed $.when
, if 3 succeeds need handle success state , 2 fails need handle failure state.
$.when($.getjson(headerurl), $.getjson(tasksurl), $.getjson(testingtrackerurl), $.getjson(highlightsurl))) .then(function(headerdata, tasksdata,testingtrackerdata,highlightsdata) { printdata(headerdata, tasksdata,testingtrackerdata,highlightsdata); }) .fail(function(data, textstatus, jqxhr) { console.error('got error in '+jqxhr); });
try
var request = function (url) { return $.getjson(url) } , requests = [ headerurl , tasksurl , testingtrackerdataurl , highlightsdataurl ]; // return array of `resolved` , `rejected` jqxhr objects $.when( $.map(requests, function (_request, i) { return request(_request) }) ) // stuff `resolved` , `rejected` jqxhr objects .always(function (arr) { $.each(arr, function (key, value) { // `success` value.then(function (data, textstatus, jqxhr) { console.log(data, textstatus, jqxhr); printdata(data) } // `error` , function (jqxhr, textstatus, errorthrown) { console.log(jqxhr, textstatus, errorthrown) }) }) });
Comments
Post a Comment