How to ensure AJAX requests called in a certain order get the response in the same order?
Date : March 29 2020, 07:55 AM
With these it helps First, keep in mind that the server itself might not return responses in the order the requests are received. Imagine if you fire a complicated ajax request first, and a simple on second (perhaps referencing some cached static data). The second request will probably return before the complicated one returns. You can see how this complicates things; if the order the requests are received is not the same as the order the responses come back, how can one make assurances? You have some options:
|
JavaScript: Ajax Requests in Order
Date : March 29 2020, 07:55 AM
Hope that helps If it is of utmost importance that they're received in the proper order, and attaching an iterating id to the form isn't enough: msg_number = 1; sendAJAX(msg_number); msg_number++;
AJAX.send({ url : "......", method : "post", success : func(){}, syncronous : true });
AJAX.send = function (obj) {
if (obj.synchronous) {
addToSyncQueue(obj); checkQueue();
} else { fireRequest(); }
};
callback = (function (old_cb) {
return function (response) {
checkQueue();
old_cb(response);
};
}(obj.success));
obj.success = callback;
AJAX.call(obj);
|
Mutiple AJAX requests out of order
Date : March 29 2020, 07:55 AM
wish help you to fix your issue The problem is in handleSuccess -- it augments jsonResultArray for every result, and invokes the callback each time: jsonResultArray.push(jsonResult)
this.fCallback(jsonResultArray);
|
Handle Ajax responses in same order as Ajax requests
Date : March 29 2020, 07:55 AM
it should still fix some issue If you create a promise up front, you could keep chaining off of it to get your desired effect. // ajax mock
function sendRequest(v, delay) {
var def = $.Deferred();
setTimeout(function () {
def.resolve(v);
}, delay);
return def.promise();
}
var ajaxPromise = $.Deferred().resolve().promise();
var delay = 600; // will decrement this with each use to simulate later requests finishing sooner
// think of this as a click event handler
function doAction (btnName) {
delay -= 100;
var promise = sendRequest(btnName, delay);
ajaxPromise = ajaxPromise.then(function () {
return promise;
}).done(function () {
console.log(btnName);
});
}
doAction("1");
doAction("2");
doAction("3");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
var ajaxPromise = $.Deferred().resolve().promise();
$('#button1').click(function(){
var promise = $.ajax({url: 'dosomething1.html'})
ajaxPromise = ajaxPromise.then(function () {
return promise;
}, function () {
return promise; // to also handle fail conditions gracefully
}).done( function(){ console.log('something 1 success'); } )
.fail( function(){ console.log('something 1 failure'); } );
});
// repeat for other two buttons
|
Order of ajax requests is always different
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , AJAX requests are asynchronous, so you cannot guarantee an order if you trigger them as siblings like this. In order to guarantee a fixed order, you need to make the subsequent call from the success block of its predecessor. Something like this: $.post('/ajax/method1', { params: params },
function(result) {
$.post('/ajax/method2', { params: params },
function(result) {
$.post('/ajax/method3', { params: params },
function(result) {
});
});
});
|