ajax拓展(带进度条)

对jquery  ajax拓展

注:代码中的NProgress是头部进度条,需要引入NProgress的代码,如果不需要进度条,则可以去掉对应的代码。

<link rel="stylesheet" href="./res/nprogress/nprogress.css">
<script src="./res/nprogress/nprogress.js"></script>

登录掉线拓展

ajaxextend.js

;(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){},
			beforeSend:function(XHR){},
			complete:function(XHR, TS){}
		}
		if(opt.error){
			fn.error=opt.error;
		}
		if(opt.success){
			fn.success=opt.success;
		}
		if(opt.beforeSend){
			fn.beforeSend=opt.beforeSend;
		}
		if(opt.complete){
			fn.complete=opt.complete;
		}
		
		//扩展增强处理
		var _opt = $.extend(opt,{
			error:function(XMLHttpRequest, textStatus, errorThrown){
				try{
					window.parent.NProgress.inc();
				}catch(e){
					NProgress.inc();
				}
				//错误方法增强处理
				fn.error(XMLHttpRequest, textStatus, errorThrown);
			},
			success:function(data, textStatus){
				try{
					window.parent.NProgress.inc();
				}catch(e){
					NProgress.inc();
				}
				var needReLogin=false;
				try{
					var data2=data; 
					if(!data.code){
						var data2=eval('(' + data + ')'); 
					}
					if(data2.code==104){
						clearAllCookie();
						window.localStorage.clear();
						needReLogin=true;
						layer.closeAll();
						layer.open({
							  title: '掉线提醒'
							  ,content: '您的账号在其它设备上登录了!!'
							  ,yes: function(index, layero){
								    //按钮【按钮一】的回调
								  console.log("---yes");
								  window.parent.location="../login2.jsp?type=clear";
							  }
							  ,cancel: function(){ 
							    //右上角关闭回调
								  console.log("---cancel");
								  window.parent.location="../login2.jsp?type=clear";
							    //return false 开启该代码可禁止点击该按钮关闭
							  }
						});    
					}else if(data2.code==100){
						clearAllCookie();
						window.localStorage.clear();
						needReLogin=true;
						layer.closeAll();
						layer.open({
							  title: '掉线提醒'
							  ,content: '您的账号登录会话过期了,请重新登录!!'
							  ,yes: function(index, layero){
								    //按钮【按钮一】的回调
								  console.log("---yes");
								  window.parent.location="../login2.jsp";
							  }
							  ,cancel: function(){ 
							    //右上角关闭回调
								  console.log("---cancel");
								  window.parent.location="../login2.jsp";
							    //return false 开启该代码可禁止点击该按钮关闭
							  }
						});    
						
					}
				}catch(e){}
				
				if(!needReLogin){
					
					//成功回调方法增强处理
					fn.success(data, textStatus);
				}
			},
			beforeSend:function(XHR){
				//console.log("----before");
				try{
					window.parent.NProgress.start(); //显示进度条
					//window.parent.NProgress.configure({ease:'ease',speed:100});
				}catch(e){
					NProgress.start(); //显示进度条
					//NProgress.configure({ease:'ease',speed:100});
				}
				fn.beforeSend(XHR);
				//window.parent.NProgress.configure({ showSpinner: false });
				//提交前回调方法
				//$('body').append("<div id='ajaxInfo' style=''>正在请求数据,请稍等...</div>");
			},
			complete:function(XHR, TS){
				//console.log("----after");
				try{
					window.parent.NProgress.done(); //完成进度条
				}catch(e){
					NProgress.done(); //显示进度条
				}
				//请求完成后回调函数 (请求成功或失败之后均调用)。
				//$("#ajaxInfo").remove();;
				fn.complete(XHR, TS);
			}
		});
		_ajax(_opt);
	};
})(jQuery);

function clearAllCookie(){
	var hostname=window.location.hostname;
	var arr=hostname.split(".");
	var domainName2=arr[arr.length-2]+"."+arr[arr.length-1];//主域名
	 var aCookie = document.cookie.split(";");
	    for (var i = 0; i < aCookie.length; i++) {
	        var aCrumb = aCookie[i].split("=");
	        var key=aCrumb[0].toString();
	        //var flag=Cookies.remove(key);
	        delCookie2(key,domainName2);
	        console.log("del cookie:key="+key);
	    }
}

function delCookie2(name, domain) {
	 document.cookie = name+'='+Cookies.get(name)+';expires='+(new Date(1))+';max-age=0;domain='+domain+';path=/';
}

 

Guess you like

Origin blog.csdn.net/u011628753/article/details/110973231