Axios global request parameter settings, request and return interceptor methods, axios global
Application scenarios:
1. parameters included in each request, such as tokens and timestamps.
2. Determine 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) {// the token has expired and is redirected to the logon page if (response. data. code = 4) {localStorage. clear () router. replace ({path: '/signin', query: {redirect: router. currentRoute. fullPath})} return response}, function (error) {// Do something with response errorreturn Promise. reject (error )})
In the above axios global request parameter settings, the request and return interceptor method is all the content that I have shared with you. I hope to give you a reference and support for the customer's house.