简体   繁体   English

使用HTML表单时如何在HTTP请求正文中发送数据?

[英]How to send data in the HTTP request body when using an HTML form?

The HTTP spec says that a POST request can contain an arbitrary body of data. HTTP规范说POST请求可以包含任意数据体。

An HTML form element can POST to a URL and may contain input elements, but those input elements get turned into a query string. HTML form元素可以POST到URL并且可以包含input元素,但这些input元素将变为查询字符串。

How can I get a form to also send along data in the body of the HTTP POST request that it sends when its submit button is pressed? 我如何获得一个form以便在按下提交按钮时发送的HTTP POST请求正文中的数据?

The HTTP spec says that a POST request can contain an arbitrary body of data. HTTP规范说POST请求可以包含任意数据体。

That's correct. 那是对的。 There are in turn however several specifications of the format of that data. 然而,该数据的格式又有几种规格。 In case of HTML forms, most commonly used is application/x-www-form-urlencoded , followed by multipart/form-data . 对于HTML表单,最常用的是application/x-www-form-urlencoded ,后跟multipart/form-data You can set it via the enctype attribute of the HTML <form> element. 您可以通过HTML <form>元素的enctype属性进行设置。 See also chapter 17.13.4 Form Content Types of the HTML specification. 另请参见第17.13.4节HTML规范的表单内容类型


An HTML form element can POST to a URL and may contain input elements, but those input elements get turned into a query string. HTML表单元素可以POST到URL并且可以包含输入元素,但这些输入元素将变为查询字符串。

That's indeed how application/x-www-form-urlencoded works. 这确实是application/x-www-form-urlencoded工作方式。 Do note that this query string actually represents the whole HTTP request body! 请注意,此查询字符串实际上代表整个HTTP请求正文! So the request body is definitely not empty as you seem to think. 因此,您似乎认为请求正文绝对不是空的。


How can I get a form to also send along data in the body of the HTTP POST request that it sends when its submit button is pressed? 我如何获得一个表单,以便在按下提交按钮时发送的HTTP POST请求正文中的数据?

It thus actually already does that. 因此它实际上已经这样做了。 If you intented to send a copy of the HTML DOM tree representation of the form itself, as somewhat hinted in the previous statement, then you can achieve that with a little help of JavaScript as follows: 如果您打算发送表单本身的HTML DOM树表示的副本,如前面的语句中稍微暗示的那样,那么您可以通过JavaScript的一点帮助实现以下目的:

<form onsubmit="this.source.value=this.outerHTML">
    ...
    <input type="hidden" name="source" />
    <input type="submit" />
</form>

The whole HTML DOM tree representation of the form is then in string format available as request parameter with name source . 然后,表单的整个HTML DOM树表示形式为字符串格式,可用作名称source请求参数。

Use javascript to send an ajax request when the button is pressed and cancel the form submission. 按下按钮时使用javascript发送ajax请求并取消表单提交。 Form submits will always be name/value pairs. 表单提交将始终是名称/值对。 XForms can send customized data but if this is for public use it'll be years before XForms is supported by the majority of browsers in use, if ever. XForms可以发送自定义数据,但如果这是供公众使用的话,那么在使用的大多数浏览器支持XForms之前还需要几年时间。

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

相关问题 如何使用nodejs服务器代码中的http请求以html形式将文件发送到邮件? - How can I send a file to mail in html form using http request in nodejs server code? 如何使用HTML按钮标签发送HTML表单POST请求? - How to send an HTML form POST request using the HTML button tag? 如何发送没有正文的html POST请求? - How to send a html POST request with no body? 如何将 HTML 表单数据作为 JSON 发送到端点 http://localhost:8080 - How to send HTML form data as JSON to endpoint http://localhost:8080 从表单中获取数据并将其作为HTTP请求发送 - Get data from a form and send it as a HTTP Request 如何在http POST请求中发送HTML代码? - How to send HTML code in an http POST request? 我们如何在表单发布中传递HTTP请求正文中的JSON数据 - How can we pass the JSON data in http request body in form post 如何使用 html 表单数据发送 JSON object - How to send a JSON object using html form data 如何使用HTML表单将数据发送和存储在XML文件中? - How to send and store data in an XML file, using an HTML form? 如何使用jquery / javascript &amp;&amp;重置html中的表单数据,以及如何使用jquery将表单数据发送到电子邮件? - How to reset the form data in html using jquery/javascript && and also how to send the form data to an email using jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM