I'm making sequential AJAX requests for my websites. There are 2 POST requests. The second request should be processed after first request is done. My code is as below:
$.ajax({
type: 'POST',
url: '/backend/edge/enableNewAgent/',
async: false,
success: function () {
console.log("First Process Done");
}
});
$.ajax({
type: 'POST',
url: '/backend/edge/deleteOldAgent/',
async: false,
success: function () {
console.log("Second Process Done");
}
});
The second process is done after first process, but the console logging is executed after second process done, not after first process done. I want the console.log is executed soon after first process done, then continue executing the second process. Can someone help ?
async:falseis kind of an oxymoron :pasync: false. It is the cause of the problem as the thread to update the console is blocked by the second AJAX request, so both items appear at the same time, after both requests have completed. To fix this use the callback pattern properly by making the second AJAX call within thesuccesshandler function of the first.async: falseyou don't yield control back to the event loop so theconsole.loglines get queued up.