ajax:如何在所有请求完成后识别当时哪个api请求失败

时间:2018-03-15 07:34:05

标签: javascript ajax

我有两个ajax调用,即

   var url1Response, url2Response;

   var ajax1 = $.ajax({
                    url: Url1,
                    success: function (response) {
                        url1Response= response;
                    }

                });

   var ajax2 = $.ajax({
                    url: Url2,
                    success: function (response) {
                        url2Response= response;
                    }
                });

       $.when(ajax1, ajax2).done(function (response1, response2) {

       });

       $.when(ajax1, ajax2).then(function (response1, response2) {

       });

ajax1,ajax2完成后:

  1. ajax1成功,ajax2成功:工作正常,内部完成/然后。
  2. ajax1成功,ajax2失败:进入内部但不在内部完成。
  3. ajax1失败,ajax2失败:进入内部,但不在里面完成。
  4. 问题是如何识别then语句中哪个API失败?而且,当我们都得到了回复时,我想要成功/失败。如果在另一个请求完成之前我获得了其中任何一个的成功/失败,那是没有用的。

1 个答案:

答案 0 :(得分:0)

$.when(ajax1, ajax2).done(function (response1, response2) {

}).fail(function(response1, response2) {
    // here you can check which response failed
});
相关问题