当响应为空时重试 axios 请求
P粉863295057
P粉863295057 2024-03-19 17:20:54
0
1
266

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 的空响应, 正在考虑如果发生这种情况重试该请求, 我想知道解决这个问题的正确方法是什么。

P粉863295057
P粉863295057

全部回复(1)
P粉556159786

我认为 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 中的它可能有点不同。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!