在某些必須要登錄才能操作的前端項目中,有時候我們用戶會遇到這樣的場景,首次訪問收藏了地址,下次通過收藏的地址訪問的時候登錄已過期,請求接口的時候后端會返回重定向的地址,此時狀態碼為200,無法從狀態碼判斷是否重定向:
這時候可以在響應攔截器中添加判斷 response.request.responseURL.match(response.config.url) 為null則跳轉登錄頁。
(loginRedirect?linkUrl=" + window.location.pathname)是傳給后端的參數,登錄后返回當前頁
service.interceptors.response.use(
response => {
console.log('response', response) // for debug
// 判斷是否
let matchUrl = response.request.responseURL.match(response.config.url);
if (matchUrl!=null) {
return response.data;
} else {
// store.dispatch("LogOut").then(() => {
window.location.href = "/login/loginRedirect?linkUrl=" + window.location.pathname;
// });
}
},
error => {
return Promise.reject(error)
}
)