我有以下代码用于默认的jQuery AJAX错误处理:
$.ajaxSetup({
error : function(jqXHR, textStatus, errorThrown) {
alert("Error: " + textStatus + ": " + errorThrown);
},
statusCode : {
404: function() {
alert("Element not found.");
}
}
});
Run Code Online (Sandbox Code Playgroud)
然而,当404发生时,BOTH函数被上调:第一个错误,然后是statusCode,所以我看到2个连续的警报.
如果statusCode没有被提升,如何防止这种行为并获得错误回调?
Gar*_*ett 26
如何在错误处理程序中检查状态代码404?
$.ajaxSetup({
error : function(jqXHR, textStatus, errorThrown) {
if (jqXHR.status == 404) {
alert("Element not found.");
} else {
alert("Error: " + textStatus + ": " + errorThrown);
}
}
});
Run Code Online (Sandbox Code Playgroud)
Bru*_*ard 11
试试这个:
$.ajaxSetup({
error : function(jqXHR, textStatus, errorThrown) {
if(jqXHR.status === 404) {
alert("Element not found.");
} else {
alert("Error: " + textStatus + ": " + errorThrown);
}
}
});
Run Code Online (Sandbox Code Playgroud)