We are making multiple ajax requests to "save" data in a web app, then reload the page.
We have run into a situation where (since requests are made asynchronously) the page is reloaded while or before the ajax calls are completed. The simple solution to this was to make the ajax calls with the "async": false option on, forcing synchronous calls. This seems to work, however dialog box code that runs BEFORE any calls are executed delay in running.
Any advice is greatly appreciated!
Also it should be noted that putting an alert() before the reload ALLOWS the ajax requests to be made. (The alert is obviously delaying the reload long enough for the requests to successfully go through)
UPDATED with code samples:
$(".submit_button").click(function(){
popupMessage();
sendData(); //the ajax calls are all in here
location.reload();
});
function sendData() {
//a bunch of these:
$.ajax({
"dataType": "text",
"type": "POST",
"data": data,
"url": url,
"success": function (msg) {}
}).done(function( msg ) {
});
}
Came across here pursuing a similar problem and decided to answer even though it's quite late for other people who might end up here with same problem.
I believe what you need is Ajax global events.
See API Documentation
Especially here;
Global Events
These events are triggered on the document, calling any handlers which
may be listening. You can listen for these events like so:
$(document).bind("ajaxSend", function(){
// You should use "**ajaxStop**" instead of "ajaxComplete" if there are more
// ongoing requests which are not completed yet
}).bind("ajaxStop", function(){
// call your reload function here
});
Now for your case, instead of binding "ajaxComplete" event if you use "ajaxStop" this will be triggered when all Ajax requests being processed are finished.
I copy-pasted your original code on fiddle and added the part I just recommended with some logs. jsfiddle.net/Tt3jk/7/ For testing purposes I called a similar SendData2()
function from within your first function's success event to simulate an ugly async request scenario. If you test this code on a real environment(or place the SendData2 with your url that responds with your data type which was "text" what you should see on the console is this output.
(1- is console.log from SendData()
and 2- is from SendData2()
):
1-sending...
waiting for all requests to complete...
1-success:!
2-sending...
waiting for all requests to complete...
1-done:
2-success:!
2-done:
completed now!
You can in fact even see it even on fiddle(with errors on the requests) when your reload function is being called. If you use "ajaxComplete", reload function inside your jQuery .click() function is being called quite early. However if you use "ajaxStop" and call reload function when "ajaxStop" event is triggered, reload function will be called after all the requests are completed.
I don't know if fiddle disappears after a while so I will post the changes I made here as well without console logs:
$(".submit_button").click(function () {
popupMessage();
sendData(); //the ajax calls are all in here
// consider reloading somewhere else
});
$(document).bind("ajaxSend", function () {
console.log("waiting for all requests to complete...");
// ajaxStop (Global Event)
// This global event is triggered if there are no more Ajax requests being processed.
}).bind("ajaxStop", function () {
// maybe reload here?
location.reload();
});
function popupMessage() {
alert("Pop!");
}
function sendData() {
//a bunch of these:
$.ajax({
"dataType": "text",
"type": "POST",
"data": "temp",
"url": "your url here!",
"beforeSend": function (msg) {
console.log("1-sending...");
},
"success": function (msg) {
console.log("1-success!");
sendData2(); // again
},
"error": function (msg) {
console.log("1-error!");
}
}).done(function (msg) {
console.log("1-done!");
});
}
function sendData2() {
//a bunch of these:
$.ajax({
"dataType": "text",
"type": "POST",
"data": "temp",
"url": "your url here!",
"beforeSend": function (msg) {
console.log("2-sending...");
},
"success": function (msg) {
console.log("2-success!");
},
"error": function (msg) {
console.log("2-error!");
}
}).done(function (msg) {
console.log("2-done!");
});
}
PS. Not sure if it's a good practice or not to make another request from within a request, probably not. But I put it there to show how "ajaxStop" event is delayed to be triggered until all ongoing requests are done(or completed with error at least)...
it depends on way you do your requests
For example (you don't do form submit. Otherwise you need prevent form submission)
$.ajax({
url: 'some_url',
type: 'GET',
data: 'var1=value1&var2=value2',
success: function(){
//do smth
},
error: function(){
alert(w.data_error);
document.location.reload();
}
complete: function(){ //A function to be called when the request finishes (after success and error callbacks are executed) - from jquery docs
//do smth if you need
document.location.reload();
}
});
Take a look onto complete block
This would help;
$("body").load("default.aspx");
Description: Load data from the server and place the returned HTML into the matched element.
http://api.jquery.com/load/