Axios 实例:如何在响应拦截器中取消请求?

时间:2021-07-06 14:38:01

标签: javascript axios interceptor

我需要在 axios 响应拦截器中对一些基于 HTTP 响应状态代码的错误进行通用的 HTTP 错误处理。 拦截器中处理的错误不应进一步传播到源处的源 HTTP 调用处理程序。

我有一个类似于下面的拦截器。这里,http 是一个使用 axios.create() -

创建的 axios 实例
http.interceptors.response.use(
    (response: AxiosResponse) => {
      return response;
    },
    (err: AxiosError) => {
      // Below error should not propogate further to the HTTP call source handlers. That is, it should not invoke success/error (then/catch) handlersof HTTP calls.
      if (err.response?.status === 401) {
        alert("Unauthorized!");
      } else {
        // Below error should propogate further to the HTTP call source handlers (working fine)
        return Promise.reject(err);
      }
    }
  );

我发现有一个 Cancel 方法可以根据帖子 here 在 axios 上使用。 但是 Cancel 方法似乎奇怪地在 axios 实例上可用。 如果我们在 axios 实例上使用拦截器,如何实现?

0 个答案:

没有答案
相关问题