轉載請注明出處:
項目中用到了單點登錄,依賴的公司通用的jar包,且項目為前后端分離的方式,為了管理系統的所有請求和
超時管理,用到了axios,做前端請求攔截,並做管理。
其有以下特點:
axios是請求后台資源的模塊,用來請求后台資源。在項目中安裝的方法為,在對應的項目路徑下,后dos窗口
執行以下命令:
npm install axios
安裝成功后會在項目的package.json文件中出現對象安裝插件的版本:
在main.js中引用改模塊:
import axios from 'axios';
開始進行攔截請求:
axios.interceptors.request.use((config) => { console.info(config); // 請求攔截 jsonp('/authStatus', null, (err, data) => { console.info(data);
// 請求攔截響應的參數,判斷是否會話過期
if(!data.hasLogin){
// 頁面刷新跳轉到登錄頁面 window.location.href = loginTimeOutUrl; } }) return config; });
攔截請求響應數據:
Vue.prototype.$http = axios; axios.defaults.crossDomain = true; axios.interceptors.response.use((response) => { if (response && response.data) { if (response.data.status == "UNKNOWN" || response.status == 302) { //alert("passport登錄超時,刷新重新登錄"); window.location.reload(); } else { return response; } } else { return response; } }, function (error) { if (302 == error.response.status || error.response.status == "UNKNOWN") { //alert("passport登錄超時,刷新重新登錄"); window.location.reload(); } else { return Promise.reject(error); } }); axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
本文參考:https://www.kancloud.cn/yunye/axios/234845