将全局回调添加到$ .ajaxSetup

cod*_*der 3 javascript ajax jquery callback

我用于$.ajaxSetup在我的站点中使用全局授权方法和全局错误处理.我想将回调函数传递给全局错误方法,这样我就可以在全局错误处理程序中随时调用该函数.这是我的$.ajaxSetup:

    $.ajaxSetup({
       global: false,
       // type: "POST",
        beforeSend: function (xhr) {
            //The string needs to be turned into 'this.pasToken'
            //if not us, don't include
            if(app.cookieAuth)
                xhr.setRequestHeader("Authorization", _this.pasToken);
        },
        statusCode: {
            401: function(){
                //Redirect to the login window if the user is unauthorized
                window.location.href = app.loginUrl;
            },
            //This is where I need help
            403: function(error, callback){
                //Show an error message if permission isn't granted
                callback(error);
                alert(JSON.parse(error.responseText)['Message']);
            }
        }
     });
Run Code Online (Sandbox Code Playgroud)

请注意403:状态代码.我试图传递回调函数,但我不知道如何从单个ajax调用中执行此操作.有人有想法吗?

Bra*_*d M 7

最简单的解决方案; 为ajax选项定义自己的属性.

$.ajaxSetup({
    statusCode: {
        403: function(error, callback){
            this.callback403(error);
        }
    }
});

$.ajax({
    callback403: function () {}
});
Run Code Online (Sandbox Code Playgroud)

请注意,如果更改contextajax请求的选项,则可能无效.