本文主要为大家带来一篇Vue-resource拦截器判断token失效跳转的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。
在拦截器中设置全局的token判断,意味着每次http请求都会校验token,与后台约定好的token过期返回码可以自定义跳转路径;
var token = window.localStorage.getItem("token");
Vue.http.interceptors.push(function(request, next) {
request.headers.set('token', token); //setting request.headers
next(function(response){
if(response.body.code===401){ //与后台约定登录失效的返回码
parent.location.href ='/login.html';
}
return response
})
})
相关推荐:
Ajax Session失效跳转登录页面的方法
vue实现登录后页面跳转到之前页面实例分享
详解PHP如何根据用户语言跳转对应网页