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