jquery 重写 ajax提交并判断权限后 使用load方法报错解决方法
废话不多说了,直接给大家贴代码了。
jQuery(function($){
//备份jquery的ajax方法
var_ajax=$.ajax;
//重写ajax方法,先判断登录在执行success函数
$.ajax=function(opt){
var_success=opt&&opt.success||function(a,b){};
var_opt=$.extend(opt,{
success:function(data,textStatus){
//如果后台将请求重定向到了登录页,则data里面存放的就是登录页的源码,这里需要找到data是登录页的证据(标记)
if((typeofdata)=='string'&&data.indexOf('shangjiaAjaxExtend')!=-1){
window.location.href='http://'+window.location.host+'/S/BusiLogin/Index';
return;
}else{
_success(data,textStatus);
}
}
});
return_ajax(_opt);
};
});
重写原理为:由于闭包函数先于页面上script加载,所以在此可以直接复写$.ajax方法.
重点是红色部分内容一定要retrun否则在使用load方法加载页面时报错“UncaughtTypeError:Cannotcallmethod'done'ofundefined。”