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 });