Synchronize three ajax requests

心不动则不痛 提交于 2019-12-23 08:50:08

问题


I have three AJAX requests firing one after another, and I'd like to be able to echo back all the data simultaneously.

$.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 all need to be loaded with their relative data at the same time. The above code isn't doing it.


回答1:


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

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


来源:https://stackoverflow.com/questions/9174267/synchronize-three-ajax-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!