为什么我必须使用POST而不是GET? [英] Why do I have to use POST instead of GET?

查看:497
本文介绍了为什么我必须使用POST而不是GET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用< form action =code.php?id = 1method =post>< / form> 时,表单id是通过URL传递。但是,当我通过用'GET'替换'POST'来编写相同的代码时,id不会传递给URL。



为什么?

$ b $当你提交一个GET表单时,表单中的值将被追加到 action URL中,作为之后的查询字符串。在这种形式的动作属性中指定一个现有的查询字符串会产生歧义。浏览器不合并这两个查询字符串,他们只是丢弃旧的查询字符串,并基于表单构建新的查询字符串。



使用POST表单,没有歧义:来自表单的数据是从URL单独发送的,所以不需要重写查询字符串。



然而,它可能是最好的不要混合这两种参数,因此解决方案总是将您的额外参数包含为隐藏字段,然后它将同时用于GET和POST表单:

 < input type =hiddenname =idvalue =1> 


When I use <form action="code.php?id=1" method="post"></form>, the form id is passed in the URL. But when I write the same code by replacing 'POST' by 'GET', the id is not passed to the URL.

Why?

解决方案

When you submit a GET form, the values from the form are appended to the action URL, as the "query string" after the ?. Specifying an existing query string in the action attribute of such a form creates an ambiguity. Browsers don't merge the two query strings, they just throw away the old query string and build the new one based on the form.

With a POST form, there is no ambiguity: the data from the form is sent separately from the URL, so there is no need for the query string to be over-written.

However, it's probably best not to mix the two kind of parameters, so the solution is always to include your extra parameters as hidden fields, then it will work with both GET and POST forms:

<input type="hidden" name="id" value="1">

这篇关于为什么我必须使用POST而不是GET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆