反应本来每个下一个帖子都没有body



我有以下问题:

每个下一个帖子呼叫获取错误:网络错误

例如,我启动该应用程序,然后单击"订阅"按钮,然后发送帖子调用并获得响应,一切正常。但是随后我单击"退订",这也发送了另一个帖子呼叫,但是这次我遇到了网络错误。

后端的家伙告诉我,没有要求供要求,请求被中止。如果我要多次单击按钮,那么所有这些请求都无法正常工作。

如果我要重新启动应用程序或等待几分钟,则第一个帖子工作,但在此之后发布

我正在使用Expo

的反应

这是我的帖子方法:

 export const apiPost = (endpoint, token, data) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url: BASE_URL + endpoint,
      headers: {
        'content-type': 'application/json',
        'authorization': token
      },
      data: data
    }).then(res => resolve(res.data))
      .catch(err => reject(err))
 });
}

以防任何人都想知道这是传递给发布的数据

的方式
const data = {
  name: "Some value"
}

这是我的软件包

"dependencies": {
  "@expo/samples": "2.1.1",
  "axios": "^0.18.0",
  "expo": "^30.0.0",
  "moment": "^2.24.0",
  "react": "16.3.1",
  "react-native": "https://github.com/expo/react-native/archive/sdk-30.0.0.tar.gz",
  "react-native-camera": "^1.2.0",
  "react-native-collapsible": "^1.3.0",
  "react-native-custom-picker": "^0.3.1",
  "react-native-datepicker": "^1.7.2",
  "react-native-keyboard-aware-scroll-view": "^0.7.2",
  "react-native-qrcode-svg": "^5.1.1",
  "react-native-root-toast": "^3.0.2",
  "react-native-search-filter": "^0.1.4",
  "react-native-segmented-control-tab": "^3.3.1",
  "react-native-simple-dialogs": "^1.1.0",
  "react-native-simple-radio-button": "^2.7.3",
  "react-native-svg": "^6.5.2",
  "react-native-svg-charts": "^5.2.0",
  "react-native-tab-view": "^1.0.2",
  "react-navigation": "^2.9.3",
  "react-redux": "^5.1.0",
  "redux": "^4.0.1",
  "redux-thunk": "^2.3.0",
  "socket.io-client": "2.0.4",
  "whatwg-fetch": "^2.0.4"

update

我做了一个测试功能,以向https://reqres.in/提出发布请求,即使在许多请求下,它也可以正常工作。因此,我想我提供的后端有一个问题。虽然后端在Angular App中正常工作

更新2

问题是在后端的HTTP2。请参阅此处:通过HTTPS返回网络错误在Android中的反应本地请求

这可能是由于您返回的承诺可能需要在实际调用功能中(尽管不确定)。

尝试使用 async ...等待更简单

export const apiPost = async (endpoint, token, data) => {
  const data = {
    name: "Some value"
  };
  try {
      const res = await axios({
          method: 'post',
          url: BASE_URL + endpoint,
          headers: {
              'content-type': 'application/json',
              'authorization': token
          },
          data
      });
      return res.json();
  }
  catch(err) {
    console.error(err);
    return err.message;
  }
}

这是React中的Codesandbox解决方案(如果我将来将其删除) -

import React, { useState } from "react";
import ReactDOM from "react-dom";
import useDidMount from "@rooks/use-did-mount";
const fakePost = async () => {
  try {
    const res = await fetch("https://jsonplaceholder.typicode.com/posts", {
      method: "POST",
      body: JSON.stringify({
        title: "foo",
        body: "bar",
        userId: 1
      }),
      headers: {
        "Content-type": "application/json; charset=UTF-8"
      }
    });
    const data = await res.json();
    return data;
  } catch (err) {
    console.error(err);
    return err.message;
  }
};
function App() {
  const [fakePostData, setFakePostData] = useState({});
  useDidMount(async () => {
    const res = await fakePost();
    console.log(res);
    setFakePostData(res);
  });
  return (
    <div className="App">
      <h1>Post fetch example</h1>
      <p>{JSON.stringify(fakePostData)}</p>
    </div>
  );
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

相关内容

  • 没有找到相关文章

最新更新