Javascript 将全局回调添加到$.ajaxSetup
我正在使用Javascript 将全局回调添加到$.ajaxSetup,javascript,ajax,jquery,callback,Javascript,Ajax,Jquery,Callback,我正在使用$.ajaxSetup在我的站点中使用全局授权方法和全局错误处理。我希望将回调函数传递到全局错误方法中,以便在需要时在全局错误处理程序中调用该函数。这是我的$。ajaxSetup: $.ajaxSetup({ global: false, // type: "POST", beforeSend: function (xhr) { //The string needs to be turned into 'thi
$.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']);
}
}
});
注意
403:
状态代码。我试图传入一个回调函数,但我不知道如何通过单个ajax
调用来实现。有人有想法吗?我不确定这是否是您想要的:
$.ajax({
statusCode: {
404: function() {
alert("page not found");
}
}
});
最简单的解决方案;为ajax选项定义自己的属性
$.ajaxSetup({
statusCode: {
403: function(error, callback){
this.callback403(error);
}
}
});
$.ajax({
callback403: function () {}
});
注意,如果更改ajax请求的上下文
选项,这可能不起作用