Home Web Front-end JS Tutorial What are the steps to set the request through axios global request parameters and return the interceptor?

What are the steps to set the request through axios global request parameters and return the interceptor?

Jun 01, 2018 am 11:59 AM
axios parameter ask

Below 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)
})
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

New feature in PHP version 5.4: How to use callable type hint parameters to accept callable functions or methods New feature in PHP version 5.4: How to use callable type hint parameters to accept callable functions or methods Jul 29, 2023 pm 09:19 PM

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

i9-12900H parameter evaluation list i9-12900H parameter evaluation list Feb 23, 2024 am 09:25 AM

i9-12900H parameter evaluation list

C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument C++ program to find the value of the inverse hyperbolic sine function taking a given value as argument Sep 17, 2023 am 10:49 AM

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

C++ function parameter type safety check C++ function parameter type safety check Apr 19, 2024 pm 12:00 PM

C++ function parameter type safety check

Analysis of common parameters and usage of numpy functions Analysis of common parameters and usage of numpy functions Jan 26, 2024 am 08:17 AM

Analysis of common parameters and usage of numpy functions

What does missing required parameters mean? What does missing required parameters mean? Sep 19, 2023 pm 03:08 PM

What does missing required parameters mean?

How Nginx implements request rewrite configuration based on request URL How Nginx implements request rewrite configuration based on request URL Nov 08, 2023 pm 04:15 PM

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. How to adjust the parameters of the beauty camera to take better-looking photos. Reference for the best parameters of the beauty camera. Mar 12, 2024 pm 02:07 PM

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

See all articles