我知道有一种方法可以在 200 HTTP 响应时使用 JQuery:
$.when( $.ajax( "test.aspx" ), $.ajax( "test2.aspx" ) ).done(function( ) {
alert( 'asd' ); // Alerts "123"
});
但是如果我得到 200 以外的响应,该如何使用它呢?例如。在哪里添加错误回调?
最佳答案
jQuery 允许您针对评论中的错误进行编程:
来自http://api.jquery.com/jQuery.ajax/
var jqxhr = $.ajax( "example.php" )
.done(function() {
alert( "success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "complete" );
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() {
alert( "second complete" );
});
如果您想自己定位状态代码,您可以沿着下面的路走,同样是从文档。
$.ajax({
statusCode: {
404: function() {
alert( "page not found" );
}
}
});
可以根据需要添加状态代码。
关于javascript - JQuery when() 出现错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20745373/