//首先备份下jquery的ajax方法
var _ajax=$.ajax;
//重写jquery的ajax方法
$.ajax=function(opt){
//备份opt中error和success方法
var fn = {
error:function(XMLHttpRequest, textStatus, errorThrown){},
success:function(data, textStatus){}
}
if(opt.error){
fn.error=opt.error;
}
if(opt.success){
fn.success=opt.success;
}
//扩展增强处理
var _opt = $.extend(opt,{
error:function(XMLHttpRequest, textStatus, errorThrown){
debugger;
erro = eval("(" + XMLHttpRequest.responseText + ")");
if(erro.err_code == 500)
alert(erro.err_msg);
//错误方法增强处理
fn.error(XMLHttpRequest, textStatus, errorThrown);
},
success:function(data, textStatus){
//成功回调方法增强处理
fn.success(data, textStatus);
},
beforeSend:function(xhr){
xhr.setRequestHeader('X-Token',$.getCookie("X-Token"));
xhr.setRequestHeader('X-Client',"PC");
xhr.setRequestHeader('Content-Type',"application/json");
}
});
return _ajax(_opt);
};
session超时处理示例:
(function ($) {
//备份ajax
var ajax_temp = $.ajax;
$.ajax = function (option) {
var complete = option.complete;
option.complete = function (XMLHttpRequest, textStatus) {
var sessionStatus = XMLHttpRequest.getResponseHeader("sessionstatus");
if (sessionStatus == 'sessionOut') {
console.log("登录超时");
return;
}
if (complete)
complete(XMLHttpRequest, textStatus);
}
return ajax_temp(option);
};
})(jQuery);
网友评论