据我所知,有三种类型:

不要使用GET和POST 不要使用POST而使用GET 你用哪一个都不重要。

我对这三种情况的假设正确吗?如果有,每个案例都有哪些例子?


当前回答

来自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.

其他回答

如果您不介意请求被重复(即它不会改变状态),请使用GET。

如果操作确实改变了系统的状态,则使用POST。

第一个重要的事情是GET和POST的含义:

GET应该用来…得到……一些来自服务器的信息, 而POST应该用来向服务器发送一些信息。

在那之后,有几件事可以注意:

使用GET,用户可以在浏览器中使用“后退”按钮,并且可以将页面添加为书签 你可以作为GET传递的参数的大小是有限制的(如果我没记错的话,某些版本的Internet Explorer是2KB);POST的限制更多,通常取决于服务器的配置。

无论如何,我不认为我们可以“生活”没有GET:想想你每天在查询字符串中使用多少带有参数的url——没有GET,所有这些都不能工作;-)

阅读维基百科上关于HTTP的文章。它将解释协议是什么以及它的作用:

得到 请求指定资源的表示形式。请注意,GET不应该用于导致副作用的操作,例如在web应用程序中使用它来执行操作。其中一个原因是,GET可以被机器人或爬虫任意使用,它们不需要考虑请求应该引起的副作用。

and

帖子 向已识别的资源提交要处理的数据(例如,来自HTML表单)。数据包含在请求体中。这可能导致创建新资源或更新现有资源,或者两者都有。

W3C有一个名为uri、Addressability的文档,以及HTTP GET和POST的使用,它解释了什么时候使用什么。引用

1.3 HTTP GET / POST选择快速检查 在以下情况下使用GET: 这种互动更像是一个问题(也就是说,它是一个问题) 安全操作,如查询、读取操作或查找)。

and

在以下情况下使用POST: 这种互动更像是一种命令,或者 交互以用户可以感知的方式改变资源的状态(例如,对服务的订阅),或者 o用户对互动的结果负责。 但是,在最终决定使用HTTP GET还是POST之前,还请考虑敏感数据和实际考虑因素。

一个实际的例子是每当您提交HTML表单时。您可以为表单操作指定post或get。PHP将相应地填充$_GET和$_POST。

我的一般经验法则是,当您向服务器发出不打算改变状态的请求时使用Get。post是为更改状态的服务器请求保留的。

最初的目的是GET用于获取数据,而POST用于任何用途。我使用的经验法则是,如果我要向服务器发送任何东西,我就使用POST。如果我只是调用一个URL来获取数据,我会使用get。