是通过表单在 HTML 中传递 POST 参数的唯一方法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/325929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is the only way of passing POST parameters in HTML through a form?
提问by Philip Morton
In HTML, you can send data from one page to another using a GET request in a couple of ways:
在 HTML 中,您可以通过以下几种方式使用 GET 请求将数据从一个页面发送到另一个页面:
http://www.example.com/somepage.php?data=1
...or...
...或者...
<form action="somepage.php" method="get">
<input type="hidden" name="data" value="1" />
<input type="submit" value="Submit">
</form>
With a POST request though, I've only seen data being sent through form elements like this:
但是,对于 POST 请求,我只看到数据是通过这样的表单元素发送的:
<form action="somepage.php" method="post">
<input type="hidden" name="data" value="1" />
<input type="submit" value="Submit">
</form>
If I only have one parameter I want to send to another page using POST, is there an easier way than wrapping it in a form?
如果我只想使用 POST 将一个参数发送到另一个页面,是否有比将其包装在表单中更简单的方法?
回答by Greg
回答by Eran Galperin
Using HTML only, a form is the only way to generate a POST request. You can use server side scripting / Javascript to generate POST requests in other ways, but no other ways to do with plain HTML only.
仅使用 HTML,表单是生成 POST 请求的唯一方法。您可以使用服务器端脚本/Javascript 以其他方式生成 POST 请求,但没有其他方式只能使用纯 HTML。
回答by Ed Lucas
As you've already discovered, there are exactly two ways to transmit data over the http protocol: GET
or POST
. There is also a third type of HTTP message called HEAD
, but that's really only used to get the meta data around a resource without downloading it and isn't widely implemented.
正如您已经发现的,有两种方法可以通过 http 协议传输数据:GET
或POST
. 还有第三种类型的 HTTP 消息称为HEAD
,但它实际上仅用于获取资源周围的元数据而不下载它,并且没有广泛实现。
Obviously both GET
and POST
are easily accessible through the use of a <form>
tag. The GET
is also easily accessible by manually adding query parameters to the URL in the form of name-value pairs (foo.html?a=1&b=2
).
显然,这两个GET
和POST
是通过使用的方便<form>
标签。的GET
也很方便通过名称-值对的形式手动添加查询参数的URL( foo.html?a=1&b=2
)。
The beauty and complexity of the POST
, however, is that the name-value pairs are communicated from the browser to the web server enclosed within the HTTP request header which is not as easily accessible. The only way to accomplish the POST
without using a <form>
tag is to manually alter the HTTP request header and add the name-value pairs in yourself.
POST
然而,的美妙和复杂之处在于,名称-值对从浏览器传送到包含在 HTTP 请求标头中的 Web 服务器,该标头不容易访问。在POST
不使用<form>
标签的情况下完成的唯一方法是手动更改 HTTP 请求标头并自己添加名称-值对。
Also keep in mind that an HTTP server doesn't intrinsically know whether a request (GET
or POST
) came from a main browser window or an AJAX call. Regardless, the web server will read the request, will decipher if it's a GET
or POST
request, look for name-value pairs as appropriate, and generate a response.
还要记住,HTTP 服务器本质上并不知道请求(GET
或POST
)是来自主浏览器窗口还是来自 AJAX 调用。无论如何,Web 服务器将读取请求,解密它是否是GET
或POST
请求,根据需要查找名称-值对,并生成响应。
If you'd like additional detail on how to properly format a POST
request you can go to jmarshall.com/easy/http/or perhaps tcpipguide.com/free/t_HTTPRequestMessageFormat.htm. The definitive resource is always the W3C, but sometimes the RFCs can be terribly confusing for us mere mortals to read.
如果您想了解有关如何正确格式化POST
请求的更多详细信息,您可以访问jmarshall.com/easy/http/或tcpipguide.com/free/t_HTTPRequestMessageFormat.htm。权威资源始终是W3C,但有时 RFC 可能会让我们这些凡人难以阅读。
回答by Patrick Desjardins
In HTML only it's with a form.
在 HTML 中只有它带有一个表单。
But you can do it if you play with your server side. Here is a good article that show you how to manipulate the Get to Change it to Postvia PHP. This will require you to play with fsockopen... This way to do it will use your parameter ?id=1¶m=2 ... and will create a POST request on the server side. You can make it generic, once it setups it will works, but it's a little work to setup everything first.
但是如果你在服务器端玩,你就可以做到。这是一篇很好的文章,向您展示了如何通过 PHP操作Get 以将其更改为 Post。这将需要您使用 fsockopen... 这种方式将使用您的参数 ?id=1¶m=2 ... 并将在服务器端创建一个 POST 请求。你可以让它通用,一旦设置它就可以工作,但是首先设置所有东西需要一些工作。
回答by Patrick Desjardins
You can of course always do a GET to a page which contains server-side (or AJAX) logic which will create a POST request (e.g. GET /pageWhichCreatesAPost.py). Very messy of course, but there can be cases where such a work-around could maybe be useful.
您当然可以始终对包含服务器端(或 AJAX)逻辑的页面执行 GET,该逻辑将创建 POST 请求(例如 GET /pageWhichCreatesAPost.py)。当然非常混乱,但在某些情况下,这种解决方法可能有用。