如何在Reactjs中处理POST请求?

huangapple go评论105阅读模式
英文:

How to handle POST request in reactjs?

问题

我有我的ReactJS应用程序运行在http://localhost:3000/。我正在接收以下表单数据作为POST请求到我的React页面

<form action="http://localhost:3000/" method="post">
  Employee Id: <input type="text" name="eid"><br>
  Department Id: <input type="text" name="did"><br>
  <input type="submit" value="Submit">
</form>

我的React应用程序应该能够处理这个POST请求并呈现以下UI

<h1>{eid}</h1>
<h1>{did}</h1>

我能够处理GET请求使用React路由器,但难以处理POST请求。我该如何实现这一点?

英文:

I have my ReactJS app running in http://localhost:3000/. I am receiving below form data to my React page as a POST request

&lt;form action=&quot;http://localhost:3000/&quot; method=&quot;post&quot;&gt;
  Employee Id: &lt;input type=&quot;text&quot; name=&quot;eid&quot;&gt;&lt;br&gt;
  Department Id: &lt;input type=&quot;text&quot; name=&quot;did&quot;&gt;&lt;br&gt;
  &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;

My react app should be able to handle this POST request and render the UI as below

&lt;h1&gt;{eid}&lt;/h1&gt;
&lt;h1&gt;{did}&lt;/h1&gt;

I am able to handle GET request using react router but struggling to handle POST request. How can I achieve this?

答案1

得分: 7

这是不可能的,如果你的React应用是静态的(不是服务器端渲染的)。当你向你的React应用发送一些POST请求时,nginx(或其他服务器)将不允许这种操作(你不能将POST请求发送到静态文件)。即使你绕过了这个限制,React脚本也不会从你的POST请求中获得任何数据,因为nginx将处理你的请求,并只返回一个包含React脚本的HTML。

英文:

That is not possible if your React app is static(not server side rendered).
When you send some POST request to your react app, nginx(or other server) will not allow that kind of action(you cannot post to static files)
Even if you bypass that restriction, react script will not have any data from your POST request, because nginx will process your request and return just a html with react script to you

答案2

得分: 0

这将不会像 PHP 那样工作,你需要有类似后端(Node.js 或 PHP 用于传递数据),甚至一些站点来接受请求。

英文:

It will not work like php.. you need to have something like backend (node or php to pass the data) or even some site to accept the request..

答案3

得分: -2

首先,您可能需要一些理论观点:

  • 首先,您应该保存数据。
  • 您将它们保存在状态中。
  • 您在渲染的部分中显示它们。

要从 API 下载数据 (GET) - 您不直接在表单中执行此操作 - 您只使用 ComponentDidMountUseEffect

componentDidMount() {
  fetch(ApiURL)
    .then(res => res.json())
    .then(res => this.setState({ planets: res }))
    .catch(() => this.setState({ hasErrors: true }));
}
useEffect(async () => {
  const result = await axios(
    ApiURL,
  );
  setData(result.data);
});

要将数据发送到 API (POST) - 这有点复杂 - 您需要关于 客户端-服务器 通信的信息。

直接来自 React 文档:

fetch('https://mywebsite.com/endpoint/', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    firstParam: 'yourValue',
    secondParam: 'yourOtherValue',
  })
})

希望这些信息对您有所帮助。

英文:

First, you need maybe some theoretical view:

https://pusher.com/tutorials/consume-restful-api-react

https://www.robinwieruch.de/react-hooks-fetch-data


  • You should firstly save data
  • You save them to the state
  • You display them in the part where it is rendered

To download data from api (GET)- you don't do it directly in form - you only use either ComponentDidMount or UseEffect.

  componentDidMount() {
    fetch(ApiURL)
      .then(res =&gt; res.json())
      .then(res =&gt; this.setState({ planets: res }))
      .catch(() =&gt; this.setState({ hasErrors: true }));
  }

  useEffect(async () =&gt; {
    const result = await axios(
      ApiURL,
    );
    setData(result.data);
  });

To send data to api (POST)- It's complicated - you need information about client-server communication

Straight from the React docs:

fetch(&#39;https://mywebsite.com/endpoint/&#39;, {
  method: &#39;POST&#39;,
  headers: {
    &#39;Accept&#39;: &#39;application/json&#39;,
    &#39;Content-Type&#39;: &#39;application/json&#39;,
  },
  body: JSON.stringify({
    firstParam: &#39;yourValue&#39;,
    secondParam: &#39;yourOtherValue&#39;,
  })
})

huangapple
  • 本文由 发表于 2020年1月6日 22:45:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614067.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定