we can do multiple call in same time. as AJAX is Async if you need to do a action after 1 or more call's success, use the below way.
Syntax
$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 ) {
// a1 and a2 are arguments resolved for the page1 and page2 ajax requests, respectively.
// Each argument is an array with the following structure: [ data, statusText, jqXHR ]
var data = a1[ 0 ] + a2[ 0 ];
if ( /Whip It/.test( data ) ) {
alert( "We got what we came for!" );
}
});
https://api.jquery.com/jquery.when/
Implementation
var async1 = DoGetCall('url 1 here');
var async2 = DoGetCall('url 2 here');
var async3 = DoGetCall('url 3 here');
var async4 = DoGetCall('url 4 here');
var async5 = DoGetCall('url 5 here');
// create call to get all exixting COR detail
$.when(async1, async2, async3, async4, async5).done(function (result1, result2, result3, result4, result5) {
if (result1[2].status === 200 && result2[2].status === 200 && result3[2].status === 200 && result4[2].status === 200 && result5[2].status === 200) {
response1 = JSON.parse(result1[2].responseText).data.employee;
response2 = JSON.parse(result2[2].responseText).data.Man;
response3 = JSON.parse(result3[2].responseText).data.d;
response4 = JSON.parse(result4[2].responseText).data.XXX;
response5 = JSON.parse(result5[2].responseText).data.YYY;
try {
globalToken = result1[2].getResponseHeader('X-CSRF-Token'); console.log(globalToken);
} catch (e) { console.log(e); }
// write the logic here...
}
else {
alert('Get failed to return');
}
}).fail(function (xhr, status, error) {
if (xhr.status === 404) {
errorMessage = error;
} else {
errorMessage = xhr.responseJSON.error.message.value;
}
});
function DoGetCall(url) {
return $.ajax({
type: 'Get',
beforeSend: function (xhr, settings) {
xhr.setRequestHeader('X-CSRF-Token', "Fetch");
},
cache: false,
url: url,
dataType: 'json',
xhrFields: {
withCredentials: true
},
crossDomain: true
});
}
// Calling
allGetCalls.push(DoGetCall('url1');
allGetCalls.push(DoGetCall('url2');
$.when.apply($, allGetCalls)
.done(function () {
$.each(arguments, function (i, data) {
console.log(data[2]); //hr is the value returned by each of the ajax requests
getOutputList.push(data[2]);
});
Syntax
$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ).done(function( a1, a2 ) {
// a1 and a2 are arguments resolved for the page1 and page2 ajax requests, respectively.
// Each argument is an array with the following structure: [ data, statusText, jqXHR ]
var data = a1[ 0 ] + a2[ 0 ];
if ( /Whip It/.test( data ) ) {
alert( "We got what we came for!" );
}
});
https://api.jquery.com/jquery.when/
Implementation
var async1 = DoGetCall('url 1 here');
var async2 = DoGetCall('url 2 here');
var async3 = DoGetCall('url 3 here');
var async4 = DoGetCall('url 4 here');
var async5 = DoGetCall('url 5 here');
// create call to get all exixting COR detail
$.when(async1, async2, async3, async4, async5).done(function (result1, result2, result3, result4, result5) {
if (result1[2].status === 200 && result2[2].status === 200 && result3[2].status === 200 && result4[2].status === 200 && result5[2].status === 200) {
response1 = JSON.parse(result1[2].responseText).data.employee;
response2 = JSON.parse(result2[2].responseText).data.Man;
response3 = JSON.parse(result3[2].responseText).data.d;
response4 = JSON.parse(result4[2].responseText).data.XXX;
response5 = JSON.parse(result5[2].responseText).data.YYY;
try {
globalToken = result1[2].getResponseHeader('X-CSRF-Token'); console.log(globalToken);
} catch (e) { console.log(e); }
// write the logic here...
}
else {
alert('Get failed to return');
}
}).fail(function (xhr, status, error) {
if (xhr.status === 404) {
errorMessage = error;
} else {
errorMessage = xhr.responseJSON.error.message.value;
}
});
function DoGetCall(url) {
return $.ajax({
type: 'Get',
beforeSend: function (xhr, settings) {
xhr.setRequestHeader('X-CSRF-Token', "Fetch");
},
cache: false,
url: url,
dataType: 'json',
xhrFields: {
withCredentials: true
},
crossDomain: true
});
}
// Calling
allGetCalls.push(DoGetCall('url1');
allGetCalls.push(DoGetCall('url2');
$.when.apply($, allGetCalls)
.done(function () {
$.each(arguments, function (i, data) {
console.log(data[2]); //hr is the value returned by each of the ajax requests
getOutputList.push(data[2]);
});
Comments
Post a Comment