Reinitiating the request url parameter in axios's request interceptor will be spliced.

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

Jul.07,2021

doubt whether your baseURL is / apis


axios (config) splices the config.baseURL in front of the config.url every time. Because it is a retry, the config.url here is already the spliced address, so leave config.baseURL blank.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b382a3-2c0e5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b382a3-2c0e5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?