据我所知,有三种类型:
不要使用GET和POST 不要使用POST而使用GET 你用哪一个都不重要。
我对这三种情况的假设正确吗?如果有,每个案例都有哪些例子?
据我所知,有三种类型:
不要使用GET和POST 不要使用POST而使用GET 你用哪一个都不重要。
我对这三种情况的假设正确吗?如果有,每个案例都有哪些例子?
当前回答
当我不希望人们看到QueryString或当QueryString变大时,我使用POST。另外,文件上传需要POST。
不过,我不认为使用GET有问题,我将它用于简单的事情,其中将内容保留在QueryString上是有意义的。
使用GET将允许链接到POST无法工作的特定页面。
其他回答
当您希望URL反映页面的状态时,请使用GET。这对于查看动态生成的页面非常有用,比如这里看到的页面。POST应该在表单中用来提交数据,就像我点击“POST Your Answer”按钮一样。它还生成了一个更干净的URL,因为它没有在路径后生成参数字符串。
因为get是纯粹的url,它们可以被web浏览器缓存,并且可能更好地用于一致生成的图像之类的事情。(设置过期时间)
一个来自gravatar页面的例子:http://www.gravatar.com/avatar/4c3be63a4c2f539b013787725dfce802?d=monsterid
GET可能会产生稍微更好的性能,一些web服务器在调用处理程序之前将POST内容写入临时文件。
另一件需要考虑的事情是尺寸限制。get的上限是URL的大小,标准是1024字节,不过浏览器可能支持更多。
传输更多的数据应该使用POST来获得更好的浏览器兼容性。
即使小于这个限制也是一个问题,正如另一个帖子所写的,URL中的任何内容都可能最终出现在浏览器UI的其他部分,比如历史记录。
来自RFC 2616:
9.3获得 GET方法意味着检索任何信息(形式为 实体)由 要求通用。如果Request-URI引用 对于数据生成过程,它是 应返回的已生成数据 作为响应中的实体而不是 进程的源文本,除非 这个文本恰好是的输出 这个过程。
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.
这就涉及到REST的概念以及web是如何被使用的。在软件工程电台上有一个很好的播客,对Get和Post的使用进行了深入的讨论。
Get用于从不需要更新操作的服务器中提取数据。其思想是,您应该能够反复使用相同的GET请求并返回相同的信息。URL在查询字符串中有get信息,因为它意味着能够轻松地发送到其他系统,人们喜欢在哪里找到一些东西的地址。
Post应该被用于(至少在web基于的REST架构中)将信息推送到服务器/告诉服务器执行一个操作。例如:更新此数据,创建此记录。
一个实际的区别是浏览器和网络服务器对URL中可以存在的字符数量有限制。每个应用程序都不一样,但如果你的表单中有文本区域,它肯定是可以达到的。
get的另一个问题是,它们会被搜索引擎和其他自动系统索引。谷歌曾经有一个产品,它可以在你正在浏览的页面上预取链接,所以如果你点击这些链接,它们会更快地加载。它对那些有delete.php之类链接的网站造成了严重破坏。Id =1 -人们失去了他们的整个网站。