如何从$中弹出错误?$. ajaxsetup .ajax实例

How can I bubble an error from an $.ajax instance to the $.ajaxSetup?

本文关键字:ajaxsetup ajax 实例 错误 出错      更新时间:2023-09-26

我在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文档中的注释:

注意:全局回调函数应该用它们各自设置. ajaxstart (), .ajaxStop(),.ajaxComplete(), .ajaxError(), .ajaxSuccess(), .ajaxSend() -而不是$. ajaxsetup()的options对象。

如果你使用ajaxError而不是ajaxSetup,你可以在全局处理程序中捕获ajaxError处理程序中的错误,并在$.ajax

的'error'参数中为特定请求添加额外的处理程序