axios reponse请求拦截以及token过期跳转问题

前两天项目中遇到了token拦截,需要在请求的header头里放置token,需要用到response拦截,调试过程中遇到了拿不到token的问题

 我用的axios实例

 let token = store.state.token
instance.interceptors.request.use(config => {

    // 在发送请求之前做些什么
    //判断是否存在token,如果存在将每个页面header都添加token
    config.headers[‘Content-Type‘] = ‘application/x-www-form-urlencoded; charset=UTF-8‘;
    // console.log(‘token===>‘,store.state.token);
    if(token!==‘‘){
        config.headers.common[‘token‘] = token;
    }
    return config;
}, error => {
    // 对请求错误做些什么
    return Promise.reject(error);
});

总是拿不token

后来根据请求步骤一步步检查发现

取token应该放到reponse拦截内,放到拦截外因为初始化登录页的时候api已经被调用,这时候还没拿到token。所以token是空的

正确的代码应该是

 instance.interceptors.request.use(config => {
    let token = store.state.token
    // 在发送请求之前做些什么
    //判断是否存在token,如果存在将每个页面header都添加token
    config.headers[‘Content-Type‘] = ‘application/x-www-form-urlencoded; charset=UTF-8‘;
    // console.log(‘token===>‘,store.state.token);
    if(token!==‘‘){
        config.headers.common[‘token‘] = token;
    }
    return config;
}, error => {
    // 对请求错误做些什么
    return Promise.reject(error);
});

完美拿到了token,可是token过期登录时总是提示 ‘router‘ is not defined 或者是 ‘router’ is not token

原因是由于单独调用的axios无法正常获取router

我用了最简单的方法,绕过router

// http response 拦截器
let self = this
instance.interceptors.response.use(
    response => {
        if (response) {
            if (response.data.status == "401") {
                    store.commit(‘del_token‘);
                    window.location.href=‘#/login‘
                    // window.routes.push({
                    //     path: ‘/login‘,

                    //     // query: {redirect: router.currentRoute.fullPath}//登录成功后跳入浏览的当前页面
                    // })
            }
        }
        return response
    },

完美解决!

原文地址:https://www.cnblogs.com/shoucigongkai/p/11763784.html

时间: 2024-07-31 05:02:16

axios reponse请求拦截以及token过期跳转问题的相关文章

axios设置请求拦截和响应拦截

首先我们先创建axios实例 const service = axios.create({ baseURL: url, //是用于请求的服务器 URL timeout: 5000, // 请求超时时间 如果请求话费了超过 `timeout` 的时间,请求将被中断 headers: {'X-Custom-Header': 'foobar'} // 自定义请求头 }); 其他属性参考:https://www.kancloud.cn/yunye/axios/234845 接下来我们来添加拦截器 //

细说vue axios登录请求拦截器

当我们在做接口请求时,比如判断登录超时时候,通常是接口返回一个特定的错误码,那如果我们每个接口都去判断一个耗时耗力,这个时候我们可以用拦截器去进行统一的http请求拦截. 1.安装配置axios cnpm install --save axios 我们可以建一个js文件来做这个统一的处理,新建一个axios.js,如下 import axios from 'axios' import { Indicator } from 'mint-ui'; import { Toast } from 'min

vue中axios 配置请求拦截功能 及请求方式如何封装

main.js 中: import axios from '................/axios' axios.js 中: //axios.js import Vue from 'vue' import axios from 'axios' Vue.prototype.$http = axios //http request 封装请求头拦截器 axios.interceptors.request.use(config => { // console.log("request&quo

Retrofit Token过期自动刷新并重新请求接口

在有心课堂的群里,有网友提出如下场景: 当前开发的 App 遇到一个问题: 当请求某个接口时,由于 token 已经失效,所以接口会报错. 但是产品经理希望 app 能够马上刷新 token ,然后重复请求刚才那个接口,这个过程对用户来说是无感的. 请求 A 接口->服务器返回 token 过期->请求 token 刷新接口->请求 A 接口 我们应该是怎么解决这个问题呢? 经过百度搜索到了相关信息,这里总结下. 本文是采用RxJava + Retrofit来实现网络请求封装. 实现原理

vue-cli项目使用axios实现登录拦截

登录拦截 一.路由拦截 项目中某些页面需要用户登录后才可以访问,在路由配置中添加一个字段requireAuth 在router/index.js中 const router = new Router({ routes: [ { //登陆 path:'/Login', component:Login }, { //用户中心 path:'/UserCenter', component:UserCenter, meta: { requireAuth: true }, } ] }) router.bef

如何解决前后端token过期问题

问题描述: 首先后端生成的token是有时限的,在一段时间后不管前端用户是否进行了访问后端的操作,后端的token都会过期,在拦截器阶段就会返回错误的请求:token过期,从而拿不到想要的请求数据. 解决思路: 每隔一段时间的后端请求中都将token传送过去获取新的token并返回前端放入cookies中并记录cookie的存储失控,达到更新cookie中token的效果;而长时间不做操作的话我们就可以让他的token失效退出系统了. 解决方式:我们的访问后端的请求都是jQuery的ajax请求

axios网络请求

axios网络请求 淘汰ajax,jquery ajax,选择axios axios名称的由来? 个人理解 axios: ajax i/o system 请求方式 axios(config) //默认请求方式 axios.request(config) axios.get(url,{config}) axios.delete(url,{config}) axios.head(url,{config}) axios.post(url,data,{config}) axios.put(url,dat

Vue+axios 实现http拦截及路由拦截

现如今,每个前端对于Vue都不会陌生,Vue框架是如今最流行的前端框架之一,其势头直追react.最近我用vue做了一个项目,下面便是我从中取得的一点收获. 基于现在用vue+webpack搭建项目的文档已经有很多了,我就不再累述了. 技术栈 vue2.0 vue-router axios 拦截器 首先我们要明白设置拦截器的目的是什么,当我们需要统一处理http请求和响应时我们通过设置拦截器处理方便很多. 这个项目我引入了element ui框架,所以我是结合element中loading和me

session过期跳转到登陆页面并跳出iframe框架的两个方法

最近在做拦截器,判断用户登录后操作超时,失去权限然后要重新登录,但是用的iframe,返回的登陆页总是在框架中显示,我百度了下,总是只有其中一个方法,现在分享下两种解决方法,希望对你们有帮助: 方法一: 一般使用filter过滤用户是否登录,如果用户没有登陆则转向登陆页面,这时候可以使用response.sendRedirect().但当在页面上使用了iframe后,发现被重定向的只是父页面中的iframe区域,登陆页面内容显示在该区域中.说明在过滤器中发送重定向请求时,是在iframe页面发送