requirement is that the interface request times out and re-initiates the request in axios"s response interceptor
the code is as follows:
axios.interceptors.response.use(response => {
}, error => {
console.log(error.config)
if(! error.config || ! error.config.retry) {
return Promise.resolve(error.response)
}
error.config.__retryCount = error.config.__retryCount || 0
if(error.config.__retryCount >= error.config.retry) {
return Promise.resolve(error.response)
} else {
error.config.__retryCount += 1
}
var backoff = new Promise(resolve => {
setTimeout(() => {
resolve()
}, error.config.retryDelay)
})
return backoff.then(r => {
return axios(error.config)
})
})
encapsulated axios request method
export default {
request(params = {method: "post", path: "", params: {}}) {
let p = {
method: params.method,
url: params.path,
baseURL: process.env.NODE_HOST,
timeout: 10,
retry: 2, //
retryDelay: 1,
headers: {
"Content-Type": "application/json;charset=UTF-8"
}
}
if("put" == p.method || "post" == p.method || patch == p.method) {
p.data = params.params
} else {
p.params = params.params
}
return axios(p)
}
}
request ({method: "post", path: "home"}) the request address initiated by the first call is / apis/home
the first call timeout or error goes to the response interceptor error to restart the request
the url parameter in the error.config printed by me on the second request will become / apis/apis/home
the third request url will become / apis/home I don"t know why