据我所知,有三种类型:
不要使用GET和POST 不要使用POST而使用GET 你用哪一个都不重要。
我对这三种情况的假设正确吗?如果有,每个案例都有哪些例子?
据我所知,有三种类型:
不要使用GET和POST 不要使用POST而使用GET 你用哪一个都不重要。
我对这三种情况的假设正确吗?如果有,每个案例都有哪些例子?
当前回答
第一个重要的事情是GET和POST的含义:
GET应该用来…得到……一些来自服务器的信息, 而POST应该用来向服务器发送一些信息。
在那之后,有几件事可以注意:
使用GET,用户可以在浏览器中使用“后退”按钮,并且可以将页面添加为书签 你可以作为GET传递的参数的大小是有限制的(如果我没记错的话,某些版本的Internet Explorer是2KB);POST的限制更多,通常取决于服务器的配置。
无论如何,我不认为我们可以“生活”没有GET:想想你每天在查询字符串中使用多少带有参数的url——没有GET,所有这些都不能工作;-)
其他回答
短的版本
GET:通常用于已提交的搜索请求,或者任何您希望用户能够再次调出确切页面的请求。
GET的优点:
url可以安全地添加书签。 页面可以安全地重新加载。
GET的缺点:
变量作为名称-值对通过url传递。(安全风险) 可以传递的变量数量有限。(基于浏览器。例如,Internet Explorer被限制为2048个字符。)
POST:用于更高安全性的请求,其中数据可能用于更改数据库,或者您不希望别人添加书签的页面。
POST的优点:
url中不显示名称-值对。(安全+= 1) 可以通过POST传递无限数量的名称-值对。参考。
POST的缺点:
使用POST数据的页面不能被书签。(如果你愿意的话。)
完整版
直接来自超文本传输协议——HTTP/1.1:
9.3 GET The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI. If the Request-URI refers to a data-producing process, it is the produced data which shall be returned as the entity in the response and not the source text of the process, unless that text happens to be the output of the process. The semantics of the GET method change to a "conditional GET" if the request message includes an If-Modified-Since, If-Unmodified-Since, If-Match, If-None-Match, or If-Range header field. A conditional GET method requests that the entity be transferred only under the circumstances described by the conditional header field(s). The conditional GET method is intended to reduce unnecessary network usage by allowing cached entities to be refreshed without requiring multiple requests or transferring data already held by the client. The semantics of the GET method change to a "partial GET" if the request message includes a Range header field. A partial GET requests that only part of the entity be transferred, as described in section 14.35. The partial GET method is intended to reduce unnecessary network usage by allowing partially-retrieved entities to be completed without transferring data already held by the client. The response to a GET request is cacheable if and only if it meets the requirements for HTTP caching described in section 13. See section 15.1.3 for security considerations when used for forms. 9.5 POST The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. POST is designed to allow a uniform method to cover the following functions: Annotation of existing resources; Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles; Providing a block of data, such as the result of submitting a form, to a data-handling process; Extending a database through an append operation. The actual function performed by the POST method is determined by the server and is usually dependent on the Request-URI. The posted entity is subordinate to that URI in the same way that a file is subordinate to a directory containing it, a news article is subordinate to a newsgroup to which it is posted, or a record is subordinate to a database. The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result.
正如其他人回答的那样,get对url大小有限制,文件只能通过post提交。
我想补充的是,一个人可以添加东西到一个数据库的get和执行操作与发布。当脚本接收到post或get命令时,它可以执行作者希望它执行的任何操作。我相信缺乏理解来自于这本书选择的措辞或你的阅读方式。
脚本作者应该使用posts来更改数据库,而只使用get来检索信息。
脚本语言提供了许多访问请求的方法。例如,PHP允许使用$_REQUEST来检索post或get。应该避免使用更具体的$_GET或$_POST。
在web编程中,有更多的解释空间。一个人应该做什么,可以做什么,但哪一个更好往往是有争议的。幸运的是,在这种情况下,没有歧义。您应该使用posts来更改数据,并且应该使用get来检索信息。
当您希望URL反映页面的状态时,请使用GET。这对于查看动态生成的页面非常有用,比如这里看到的页面。POST应该在表单中用来提交数据,就像我点击“POST Your Answer”按钮一样。它还生成了一个更干净的URL,因为它没有在路径后生成参数字符串。
如果您不介意请求被重复(即它不会改变状态),请使用GET。
如果操作确实改变了系统的状态,则使用POST。
一个实际的区别是浏览器和网络服务器对URL中可以存在的字符数量有限制。每个应用程序都不一样,但如果你的表单中有文本区域,它肯定是可以达到的。
get的另一个问题是,它们会被搜索引擎和其他自动系统索引。谷歌曾经有一个产品,它可以在你正在浏览的页面上预取链接,所以如果你点击这些链接,它们会更快地加载。它对那些有delete.php之类链接的网站造成了严重破坏。Id =1 -人们失去了他们的整个网站。