(function($){
//备份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,{
//在此处设置请求头
beforeSend:function(xhr){
setRequestHeaders(xhr);
},
//在此处设置error
error:function(XMLHttpRequest, textStatus, errorThrown){
//错误方法增强处理
var result=null;
if (XMLHttpRequest.responseText && XMLHttpRequest.responseText !="") {
result=$.parseJSON(XMLHttpRequest.responseText);
}else if (XMLHttpRequest.responseJSON && XMLHttpRequest.responseJSON != "" && XMLHttpRequest.responseJSON != null) {
result=$.parseJSON(XMLHttpRequest.responseJSON);
}
if (result !=null && result.code == "401") {
alert("登录已经超时,请重新登录!",function(){
$.removeCookie('jaAuthorization');
$.removeCookie('jaloginName');
$.removeCookie('japassword');
$.removeCookie('jaUserId');
$.removeCookie('jaUserType');
window.parent.location.href='../login.html';
});
return ;
}else if(result !=null && result.code == "403"){
alert("没有权限,请联系管理员",function(){
});
return ;
}else if (result !=null) {
alert(result.desc,function(){});
return ;
}
fn.error(XMLHttpRequest, textStatus, errorThrown);
}
});
_ajax(_opt);
};
})(jQuery);