javascript - Handling multiple jQuery Ajax requests -
i trying handle aborted status in using jquery/ajax.
i making multiple ajax calls. if 1 of ajax call success (like 1 of status
200 ok
in snapshot) need display success message. if ajax call aborted
status, need display failure message.
but not working way; hence bit confused. please guide.
i think, can solve problem without async:false
. use promise synchronize multiple requests:
$(function() { $("#btnsubmit").click(function() { var dfd = new $.deferred(); var proms = []; var errs = 0; (var = 8078; < 8085; i++) { var port = i; proms.push(jquery.ajax({ url: 'http://localhost:' + port + '/test/', datatype: 'jsonp' }) .then(function(resp) { dfd.resolve(resp); return resp; }, function ( err ) { errs++; if ( errs === proms.length ) dfd.reject( err ); })); } dfd.promise() .then(function ( resp ) { // todo: add logic }, function () { console.log() if ( errs === proms.length ) console.log( 'all fails' ); }); }) });
Comments
Post a Comment