请求取消时获取Axios拦截器中的请求参数

时间:2021-05-28 12:59:05

标签: javascript axios

我有一个拦截器:

axios.interceptors.response.use(doSomething, error => handleError(error));

我可以在 err.config 中访问有关“正常”错误的请求的所有信息

const handleError = err => {
   if(err.config) {
      // use values from err.config
   }
}

然而,Cancel 错误不包含它。它只包含 message

是否可以在请求被取消的情况下访问拦截器中的请求参数?

1 个答案:

答案 0 :(得分:0)

如果您在处理 cancel 事件时特别需要某些信息 - 您需要在取消请求的地方提供该信息:

const token = CancelToken.source()
const request = axios.get('/url', { cancelToken: token }).then(....);
token.cancel(
... put here the information that will be available as the `err.message` parameter in the `cancel` interceptor
);