


What are the steps to set the request through axios global request parameters and return the interceptor?
Jun 01, 2018 am 11:59 AMBelow I will share with you an article on axios global request parameter settings, request and return interceptor methods, which has a good reference value and I hope it will be helpful to everyone.
Application scenarios:
1. Each request carries parameters, such as token, timestamp, etc.
2, judge the returned status, such as whether the token has expired
The code is as follows:
axios.interceptors.request.use( config => { var xtoken = getXtoken() if(xtoken != null){ config.headers['X-Token'] = xtoken } if(config.method=='post'){ config.data = { ...config.data, _t: Date.parse(new Date())/1000, } }else if(config.method=='get'){ config.params = { _t: Date.parse(new Date())/1000, ...config.params } } return config },function(error){ return Promise.reject(error) } ) axios.interceptors.response.use(function (response) { // token 已过期,重定向到登录页面 if (response.data.code == 4){ localStorage.clear() router.replace({ path: '/signin', query: {redirect: router.currentRoute.fullPath} }) } return response }, function (error) { // Do something with response error return Promise.reject(error) })
The above is what I compiled for everyone, I hope It will be helpful to everyone in the future.
Related articles:
Sample code to implement image and file upload in vue
Simple example of Vue implementing search and news list functions
Methods of using axios in vue components
The above is the detailed content of What are the steps to set the request through axios global request parameters and return the interceptor?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

New feature in PHP version 5.4: How to use callable type hint parameters to accept callable functions or methods

C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument

C++ function parameter type safety check

Analysis of common parameters and usage of numpy functions

What does missing required parameters mean?

How Nginx implements request rewrite configuration based on request URL

How to adjust the parameters of the beauty camera to take better-looking photos. Reference for the best parameters of the beauty camera.
