Ajax请求Session超时的解决办法:拦截器 + 封装jquery的post方法
生活随笔
收集整理的這篇文章主要介紹了
Ajax请求Session超时的解决办法:拦截器 + 封装jquery的post方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目標:前端系統,后端系統等,統一處理Session超時和系統錯誤的問題。
可能需要處理的問題:Session超時、系統500錯誤、普通的業務錯誤、權限不足。
同步請求:
? ? ? ? ? ?Session超時,登錄攔截器,重定向到登錄頁面。
? ? ? ? ? ?500等系統錯誤,SpringMVC自定義ExceptionHanlder,跳轉到系統錯誤頁面,給訪客友好的提示。?
? ? ? ? ? ? 業務錯誤和權限不足,與500類似。
? ? ? ? ? ? 相對而言,同步方式,處理起來還是比較容易的。
異步請求:?
? ? ? ? 2種方式
1.后端登錄攔截器,發現Session超時,給個標記。
? ?后端系統錯誤捕捉,給個標記。
? 比如:
??if (CommonUtil.isAjaxRequest(request)) {response.setHeader(BERROR, "yes"); CommonUtil.jsonError(response);return null;}
? 前端
??<script>$(function() {$.ajaxSetup({contentType : "application/json;charset=utf-8",complete : function(XMLHttpRequest, textStatus) {// 通過XMLHttpRequest取得響應頭,var blogin = XMLHttpRequest.getResponseHeader("blogin");if (blogin == "yes") {alert("Session time out2");window.location = "${frontLoginUrl}";}var berror = XMLHttpRequest.getResponseHeader("berror");if (berror == "yes") {alert("Session error2");}}}); }); </script>
2. 封裝jquery的post方法。
??function post(url, params, fn) {$.post(url, params, function(data) {if(data.code==-1){alert("Session time out");return;}if(data.code==-5){alert("System error.");return;}if(data.code ==0){alert("出了點小問題,"+data.msg);return;}if(data.code == -2){alert("權限不足,請聯系網站管理員.");return;}fn(data);}); }
? ?調用方式:
??post(base + "/home/bankcard/doadd.json", {"bank" : bank,"card" : card}, function(data) { }
參考資料:http://daichangfu.iteye.com/blog/1705097、某前端王的建議
補充幾點
1.后端攔截器加標記。
? ?好處是:前端用ajaxSetup,前端沒有干擾到任何已有的代碼。比如原來$.post不用修改。
? ?壞處是:后端要改。
2.前端統一封裝post,正好反過來。?
? ?前端需要修改已有的代碼。?
? ?后端不需要修改。?
可能需要處理的問題:Session超時、系統500錯誤、普通的業務錯誤、權限不足。
同步請求:
? ? ? ? ? ?Session超時,登錄攔截器,重定向到登錄頁面。
? ? ? ? ? ?500等系統錯誤,SpringMVC自定義ExceptionHanlder,跳轉到系統錯誤頁面,給訪客友好的提示。?
? ? ? ? ? ? 業務錯誤和權限不足,與500類似。
? ? ? ? ? ? 相對而言,同步方式,處理起來還是比較容易的。
異步請求:?
? ? ? ? 2種方式
1.后端登錄攔截器,發現Session超時,給個標記。
? ?后端系統錯誤捕捉,給個標記。
? 比如:
??if (CommonUtil.isAjaxRequest(request)) {response.setHeader(BERROR, "yes"); CommonUtil.jsonError(response);return null;}
? 前端
??<script>$(function() {$.ajaxSetup({contentType : "application/json;charset=utf-8",complete : function(XMLHttpRequest, textStatus) {// 通過XMLHttpRequest取得響應頭,var blogin = XMLHttpRequest.getResponseHeader("blogin");if (blogin == "yes") {alert("Session time out2");window.location = "${frontLoginUrl}";}var berror = XMLHttpRequest.getResponseHeader("berror");if (berror == "yes") {alert("Session error2");}}}); }); </script>
2. 封裝jquery的post方法。
??function post(url, params, fn) {$.post(url, params, function(data) {if(data.code==-1){alert("Session time out");return;}if(data.code==-5){alert("System error.");return;}if(data.code ==0){alert("出了點小問題,"+data.msg);return;}if(data.code == -2){alert("權限不足,請聯系網站管理員.");return;}fn(data);}); }
? ?調用方式:
??post(base + "/home/bankcard/doadd.json", {"bank" : bank,"card" : card}, function(data) { }
參考資料:http://daichangfu.iteye.com/blog/1705097、某前端王的建議
補充幾點
1.后端攔截器加標記。
? ?好處是:前端用ajaxSetup,前端沒有干擾到任何已有的代碼。比如原來$.post不用修改。
? ?壞處是:后端要改。
2.前端統一封裝post,正好反過來。?
? ?前端需要修改已有的代碼。?
? ?后端不需要修改。?
轉載于:https://www.cnblogs.com/qitian1/p/6462808.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Ajax请求Session超时的解决办法:拦截器 + 封装jquery的post方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: asp.net实现GZip压缩和GZip
- 下一篇: c++出错记录