ExtJS以及JQuery异步请求Session过时解决方案html
1 //经过Ext封装的ajax请示时,默认增长请求头 2 //或者可使用全部ajax请求都带有的x-request-with:XMLHttpRequest头信息 3 //若是既有Ext又有Jquery并且要区分处理,则须要这种方式 4 Ext.Ajax.defaultHeaders = { 5 'Request-By': 'Ext' //标识ajax请求 6 }; 7 //当响应时,自动拦截并判断若是符合timeout为true就重定位到redirectUri 8 Ext.Ajax.on('requestcomplete',checkSessionStatus, this); 9 function checkSessionStatus(conn,response,options){ 10 var json = Ext.decode(response.responseText); 11 if(typeof json == 'object' 12 && json.timeout){ 13 Ext.Msg.alert("提示","登入超时,系统将自动跳转到登录页面,请从新登入!",function(){ 14 top.window.location.href = json.redirectUri; 15 }); 16 } 17 }
1 String vsResuqestBy = request.getHeader("Request-By"); 2 String redirectUri = request.getContextPath() + "/login.jsp"; 3 //若是是Ext的ajax请求则返回响应{"timeout":true,"redirectUri":"/ServletContext/login.jsp"} 4 //不然,则直接重定为到login.jsp 5 if(vsResuqestBy != null && "Ext".endsWith(vsResuqestBy)){ 6 response.getWriter().write("{\"timeout\":true,\"redirectUri\":\""+redirectUri+"\"}"); 7 } 8 else{ 9 response.sendRedirect(redirectUri); 10 }
1 $.ajaxSetup({ 2 headers: { 3 'Request-By': 'Jquery' 4 } 5 }); 6 7 $.ajaxComplete(function(event,xhr,settings){ 8 var json = eval('('+xhr.responseText+')'); 9 if(typeof json == 'object' 10 && json.timeout){ 11 alert("登入超时,系统将自动跳转到登录页面,请从新登入!"); 12 top.window.location.href = json.redirectUri; 13 } 14 });