Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将多个ajax调用绑定到jQuery.when()中时如何处理错误?_Jquery_Ajax - Fatal编程技术网

将多个ajax调用绑定到jQuery.when()中时如何处理错误?

将多个ajax调用绑定到jQuery.when()中时如何处理错误?,jquery,ajax,Jquery,Ajax,我想等待多个AJAX调用,而不将一个调用嵌套在另一个调用中 从文档中,我了解到可以使用jQuery.when() 但是,从提供的示例中,我不理解如何处理错误。在一个普通的jquery.ajax调用中,我在选项中提供了一个error回调 理想情况下,我希望在至少一个ajax调用导致错误时调用提供的错误回调。正确的做法是将链接到,然后在时将链接到,而不是完成 例如: $.when( $.ajax("/page1.php", { error: function(jqXHR, textSt

我想等待多个AJAX调用,而不将一个调用嵌套在另一个调用中

从文档中,我了解到可以使用
jQuery.when()

但是,从提供的示例中,我不理解如何处理错误。在一个普通的
jquery.ajax
调用中,我在选项中提供了一个
error
回调


理想情况下,我希望在至少一个ajax调用导致错误时调用提供的错误回调。

正确的做法是将
链接到
,然后在
时将
链接到
,而不是
完成

例如:

$.when(
   $.ajax("/page1.php", {
    error: function(jqXHR, textStatus, errorThrown){
      // handle the error for this specific call
    },
    success: function(data, textStatus, jqXHR){
      // handle the success for this specific call
    }
   }), 
   $.ajax("/page2.php"), {
    error: function(jqXHR, textStatus, errorThrown){
      // handle the error for this specific call
    },
    success: function(data, textStatus, jqXHR){
      // handle the success for this specific call
    }
   }).then(
   function(){
     // all calls succeeded
   },
   function(){
     // at least one of the calls failed
   }
});

正确的做法是在
时将
然后
链接到
,而不是
完成

例如:

$.when(
   $.ajax("/page1.php", {
    error: function(jqXHR, textStatus, errorThrown){
      // handle the error for this specific call
    },
    success: function(data, textStatus, jqXHR){
      // handle the success for this specific call
    }
   }), 
   $.ajax("/page2.php"), {
    error: function(jqXHR, textStatus, errorThrown){
      // handle the error for this specific call
    },
    success: function(data, textStatus, jqXHR){
      // handle the success for this specific call
    }
   }).then(
   function(){
     // all calls succeeded
   },
   function(){
     // at least one of the calls failed
   }
});

您可以在
a1[0]
a2[0]
中获得结果。然后您可以测试
“成功”
“错误”
。您确定吗?我已经尝试过了,但是传递给
done
的函数没有被调用。您可以在
a1[0]
a2[0]
中获得结果。然后您可以测试
“成功”
“错误”
。您确定吗?我已经尝试过了,但是传递给
done
的函数没有被调用。这似乎对我使用jquery-2.0.3不起作用。这似乎对我使用jquery-2.0.3不起作用