Sync three ajax requests

I have three AJAX requests that run one after another, and I would like to be able to repeat all the data at once.

$.ajax ({ type: "POST", url: "page1.php", data: "var1=" + var1, success: function(msg) { $("#results2").load("page2.php", function (responseText, textStatus, XMLHttpRequest) { $("#results3").load("page3.php", function (responseText, textStatus, XMLHttpRequest) { if (textStatus == "success") { $("#results1").html(msg); } }); }); } }); 

#results1 , #results2 and #results3 should all be loaded together with their relative data at the same time. The above code does not do this.

+7
source share
1 answer

you can use the jQuery deferred object added in version 1.5:

 $.when( $.ajax({1}) , $.ajax({2}) , $.ajax({3}) ) .then(function() { alert("tada"); }); 
+10
source

All Articles