Axios拦截器-第二个请求跳过方法的某些部分

时间:2018-08-08 12:52:11

标签: axios interceptor refresh-token

我正在使用拦截器来捕获401错误并刷新令牌。下一个我想重试上一个请求,所以我这样做:

axios.interceptors.response.use(function (response) {
return }, function (error) {
let res = error.response
if (res.status === 401) {
  let model = LocalStorage.get.item('account')
  let passingModel = {
    token: model.token,
    refreshToken: model.refreshToken
  }
  userLogic.refreshToken(passingModel, LocalStorage.set, axios.request, console.log, error)
}
return Promise.resolve(error)  })

像这样启动userLogic.refreshToken方法:

refreshToken (model, responseAction, secondRequest, errorAction, error) {
debugger
var authOptions = {
  method: 'POST',
  url: Const.ApiPath + Const.RefreshPath,
  data: model,
  headers: {
    'Content-Type': 'application/json'
  },
  json: true
}
Axios(authOptions)
  .then(function (response) {
    responseAction('account', response.data)
    let newHeader = LocalStorage.get.item('account').token
    error.config.headers.Authorization = newHeader ? `Bearer ${newHeader}` : ''
    return secondRequest(error.config)
  })
  .catch(function (error) {
    errorAction(error)
  })  }

所以在refreshToken方法中,我执行secondRequest(error.config),而它只是axios.request方法。我的重试方法例如:

getAllClients (responseAction) {
debugger
console.log('from get all')
var authOptions = {
  method: 'GET',
  url: Const.ApiPath + Const.GetAllClientsPath,
  headers: {
    'Content-Type': 'application/json'
  },
  json: true
}

Axios(authOptions)
  .then((response) => {
    debugger
    responseAction(response.data)
  })
  .catch(function (error) {
    debugger
    console.log(error)
  })  }

,该方法会得到一些响应,但不要进入“ .then”块-这就是我基于响应的更新视图的一部分。因此,我必须手动刷新页面以获取新视图。如何使用“ .then”部分重试请求?或如何以其他方式更新视图?

0 个答案:

没有答案