繁体   English   中英

响应为空时重试 axios 请求

[英]Retry axios request when response is empty

  await axios
    .post(
      executeBatch,
      {
        headers: {
          "Content-Type": "application/json",
          "x-access-token": localStorage.getItem("token"),
        },
      }
    )
    .then(
      (response) =>
        this.$store.commit(
          "insertOutputFile",
          response.data.outputFile._id
        ),
    );

  alert("You can download the result");

所以有时我收到状态码为 200 的空响应,正在考虑如果发生这种情况重试请求,我想知道解决此问题的正确方法是什么。

我认为 axios 拦截器会为你工作。

 axios.interceptors.response.use((response) => { return response }, async function(error) { const originalRequest = error.config; if () { // condition originalRequest._retry = true; } } )

您可以在基本 redux 目录中创建 setupAxios 文件,并将其从基本 redux 目录中的 index.js 导出。

export {default as setupAxios} from "./setupAxios";

并从您的根 index.js 文件中定义 setupAxios

import * as _redux from "./redux";

_redux.setupAxios(axios, store);

顺便说一句,我使用的是 react.js,它在 vue.js 中可能会有所不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM