gpt4 book ai didi

jQuery ajax - 在调用 statusCode() 回调时避免 error() 回调

转载 作者:行者123 更新时间:2023-12-03 21:54:40 26 4
gpt4 key购买 nike

我正在使用 jQuery.ajax() 连接到我的后端服务。我已经配置了一个 error() 处理程序和一个 statusCode() 处理程序。它们都工作正常,但是当我的 statusCode 处理程序被触发时,错误处理程序也会触发(错误处理程序实际上首先被触发)。我希望这种情况不要发生。我假设这是可能的,而无需破解错误处理程序代码?

我的代码看起来像这样:

$.ajax({
...
error: function(...) {
// process a general type of error here
},
statusCode: {
401: function() {
// process a specific authentication failure
}
}
});

那么如何避免当 HTTP 状态代码为 401 时触发 error() 处理程序?

感谢您花时间阅读!

最佳答案

您可以轻松检查错误回调中的状态代码。第一个参数应该是 XMLHttpRequest (或 jqHXR 取决于您的 jQuery 版本)对象。相应地检查错误句柄的状态代码的“状态”属性。

例如

error: function(a, b, c){
if(a.status != 401){
// handle other errors except authentication

}
}

关于jQuery ajax - 在调用 statusCode() 回调时避免 error() 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9465067/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com