axios re-initiate the last request

created at 08-17-2021 views: 4

axios re-initiate the request, generally used to re-initiate the request

1. Switch domain name and try again:

  const response = error.response
  const config = response.config
  config.baseURL = api_2
  console.log('err', error)
  return new Promise((resolve, reject) =>
    setTimeout(() => {
      try {
        resolve(axios(config))
      } catch (err) {
        reject(err)
      }
    }, 1 * 1000))

2. Retry times control:

//Response interceptor
axios.interceptors.response.use((response) => {
  const res = response.data
  if (res.code == 10000) {
    return res.data
    //return Promise.resolve(res.data)
  } else {
    console.warn(res.msg);
    return Promise.reject(res)
  }
}, (error) => {
  const response = error.response
  const config = response.config
  const code = response.status.toString().substr(0, 1)
  if (code == "4" && api_count > 0) {
    api_count--
  } else {
    if (config.baseURL == api_2) {
      return
    }
    config.baseURL = api_2
  }
  const $throw=Vue.prototype.$throw
  $throw(error)
  return new Promise((resolve, reject) =>
    setTimeout(() => {
      try {
        resolve(axios(config))
      } catch (err) {
        reject(err)
      }
    }, 1 * 1000))
})
created at:08-17-2021
edited at: 08-17-2021: