简体   繁体   English

如何在没有外部 API 的情况下在 React 中发布表单数据

[英]How to POST form data in React without external API

I am working with Amazon's Mechanical Turk, trying to build an ExternalQuestion site which POSTs data back to AMT using a form-- this is the typical method of passing back answers from an ExternalQuestion, mandated by their API.我正在与 Amazon 的 Mechanical Turk 合作,尝试构建一个 ExternalQuestion 站点,该站点使用表单将数据发回 AMT——这是从其 API 要求的 ExternalQuestion 传回答案的典型方法。

Very specifically, I am trying to do this in ReactJS, because it has simplified every other aspect of this project.非常具体地说,我试图在 ReactJS 中做到这一点,因为它简化了这个项目的所有其他方面。

Is it possible to get React to POST form data without using an external back-end like Flask/python?是否可以在不使用 Flask/python 等外部后端的情况下将 React 发送到 POST 表单数据?

This is an important requirement because as far as I can tell from this information (and my own wasted time), using Flask/python will make the POST data look like it is coming from my server, rather than the Worker's browser, and will get rejected.这是一个重要的要求,因为据我从这些信息(以及我自己浪费的时间)可以看出,使用 Flask/python 将使 POST 数据看起来像是来自我的服务器,而不是 Worker 的浏览器,并且会得到拒绝了。

And yet, when I look through the React documentation on forms I don't even see a discussion of form methods, like GET and POST.然而,当我查看有关表单React 文档时,我什至没有看到对表单方法的讨论,例如 GET 和 POST。 I understand that React is going to want this handled by something like an onClick() function, but I can't see any way to do this in React without making the data look like it's coming from my server rather than Worker's browser.我知道 React 会希望通过诸如 onClick() 函数之类的东西来处理这个问题,但是我看不出有任何方法可以在 React 中做到这一点,而不会使数据看起来像是来自我的服务器而不是 Worker 的浏览器。

Form post is just XHR POST with form-data or x-www-form-urlencoded body and get text/HTML returned.表单发布只是带有表单数据或 x-www-form-urlencoded 正文的 XHR POST 并返回文本/HTML。 This can be done on React with Axios.这可以在带有 Axios 的 React 上完成。

This answer show using Axios to send form-data -> https://stackoverflow.com/a/47630754/3849555此答案显示使用 Axios 发送表单数据 -> https://stackoverflow.com/a/47630754/3849555

Your best shot is to use the built in JavaScript Fetch API with the FormData interface.最好的办法是使用内置的 JavaScript Fetch APIFormData接口。

For the body you can pass in the payload what you can generate with the FormData interface;对于正文,您可以传入可以使用FormData接口生成的有效负载; MDN's documentation on it: MDN 关于它的文档:

The FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest.send() method. FormData 接口提供了一种轻松构造一组表示表单字段及其值的键/值对的方法,然后可以使用 XMLHttpRequest.send() 方法轻松发送这些键/值对。 It uses the same format a form would use if the encoding type were set to "multipart/form-data".如果编码类型设置为“multipart/form-data”,它使用与表单相同的格式。

Don't use XMLHttpRequest , fetch is the newer that is built on that.不要使用XMLHttpRequest , fetch 是基于它构建的较新的。

A Generic example with fetch would look like the following:带有 fetch 的通用示例如下所示:

const formData = new FormData();

formData.append('username', 'abc123');
formData.append('foo', 'bar);

fetch('https://example.com/foo/bar', {
  method: 'POST',
  body: formData
})
.then(response => response.json())
.catch(error => console.error('Error:', error))
.then(response => console.log('Success:', JSON.stringify(response)))

This fetch call then can be called based on a user action like onClick .然后可以根据用户操作(如onClick调用此 fetch 调用。

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

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