我在ajax设置中定义了一个错误回调,它将在每个ajax实例中执行:
$.ajaxSetup({
cache: false,
error: function (data, textStatus, pStatusDescription) {
if (pStatusDescription === "Unauthorized")
{
// show dialog
alert("Your Login has expired. Please re-login.");
return;
}
}
});
我还有一个简单的 ajax 请求,它有自己的错误处理定义。此定义会覆盖 $.ajaxSetup
中的定义:
$.ajax({
url: "http://hostname",
success: function(xhr) { ... },
error: function(data, textStatus, pStatusDescription) {
if(pStatusDescription === "ObjectNotFound")
{
// remove Object
return;
}
// call 'error' callback in $.ajaxSetup now.
}
});
如何将错误从 $.ajax
实例冒泡到 $.ajaxSetup
?
最佳答案
我认为问题是:您使用ajaxSetup的方式错误。查看ajaxSetup docs中的注释:
Note: Global callback functions should be set with their respective global Ajax event handler methods—.ajaxStart(), .ajaxStop(), .ajaxComplete(), .ajaxError(), .ajaxSuccess(), .ajaxSend()—rather than within the options object for $.ajaxSetup().
所以如果你ajaxError您可以在全局处理程序的 ajaxError 处理程序中捕获错误,而不是 ajaxSetup,并在 $.ajax 的 'error' 参数中为特定请求添加其他处理程序
关于javascript - 如何将错误从 $.ajax 实例冒泡到 $.ajaxSetup?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39916367/