SoFunction
Updated on 2025-04-04

Axios global request parameter setting, request and return method of interceptor

Application scenarios:

1. Each request contains parameters, such as token, timestamp, etc.

2. Make a judgment on the returned status, such as whether the token has expired

The code is as follows:

(
		config => {
			var xtoken = getXtoken()
			if(xtoken != null){
				['X-Token'] = xtoken
			}
			if(=='post'){
				 = {
					...,
					_t: (new Date())/1000,
				}
			}else if(=='get'){
				 = {
					_t: (new Date())/1000,
					...
				}
			}
			return config
		},function(error){
			return (error)
		}
	)
(function (response) {
	// token has expired, redirect to login page	if ( == 4){
		()
		({
            path: '/signin',
            query: {redirect: }
          })
	}
	return response
}, function (error) {
	// Do something with response error
	return (error)
})

The above article on axios global request parameter setting, request and return to the interceptor is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.