[转]多个ajax请求时控制执行顺序或全部执行后的操作

本文转自:https://blog.csdn.net/fsdad/article/details/71514822

一、当确保执行顺序时:

1、 请求加async: false,,这样所有的ajax就会同步执行,请求顺序就是代码顺序

2、$.when($.ajax(
            {async: false,
                url : url1
            }
        ), $.ajax(
            {async: false,
                url : url2
            }
      )).done(function(){

        alert("done");

    }).fail(function(){

        alert("fail");

    });

二、确保所有异步的ajax请求完毕时

    1、 ajax6 = $.ajax(
      {
     url: "/Home2/SelectyjLoginuser",
     dataType: "json",
     type: "post",
     success: function (paraResponse) {
     }
      });
        ajax7 = $.ajax(
      {
          
           url: "/Home2/Selectyjzh",
           dataType: "json",
           type: "post",
           success: function (paraResponse) {
           }
      });

//确保(ajax6, ajax7两个请求完毕时执行
        $.when(ajax6, ajax7).done(function () {
          //所做操作
        });
    }

2、上述的第二种同样也可以用

 

 

posted on 2018-07-18 15:24  freeliver54  阅读(468)  评论(0编辑  收藏  举报

导航