React fetch 请求被取消

时间:2021-05-11 18:11:03

标签: reactjs express fetch

我正在尝试创建一个允许用户从 GitHub API 返回数据的小型应用程序,我有一个 React 应用程序可以从快速服务器获取其数据。

当我从 Postman 调用 express 路径时,数据返回正常,所以我认为调用本身不是问题。

app.js

app.post("/GHIssueBeautifier/issues", (req, res) => {
  const { repo } = req.body;
  dotenv.config();
  fetch(`https://api.github.com/repos/ORGNAME/${repo}/issues`, {
    headers: {
      Authorization: `token ${process.env.PERSONAL_ACCESS_TOKEN}`,
    },
    method: "GET"
  })
    .then((response) => response.json())
    .then((response) => {
      return res.status(200).json(response);
    })
    .catch((error) => {
      return res.status(400).json(error);
    });
});

我已经使用 console.logs 来检查数据是否正确返回并且看起来是这样。

Home.js

const searchRepo = () => {
      const requestData = {
        repo: this.state.repo,
      };
      fetch(`${process.env.REACT_APP_API_URL}GHIssueBeautifier/issues`, {
        method: "POST",
        body: JSON.stringify(requestData)
      })
        .then((response) => { 
          response.json()
        })
        .then((data) => {
          data.forEach(element => {
            element.milestone_search = this.state.milestone
          });
          this.setState({ items: data });
        })
        .catch((error) => {
          console.log('Error', error);
        });
    };

这个获取总是返回错误:

Error returned

Chrome 中的网络工具显示请求已被取消:

Chrome network tools

非常感谢任何帮助。

0 个答案:

没有答案