POST 和 GET 有什么区别? [英] What is the difference between POST and GET?

查看:22
本文介绍了POST 和 GET 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近才开始接触 PHP/AJAX/jQuery,在我看来,这些技术的一个重要部分是 POSTGET.

I've only recently been getting involved with PHP/AJAX/jQuery and it seems to me that an important part of these technologies is that of POST and GET.

首先,POSTGET 有什么区别?通过实验,我知道 GET 将返回的变量及其值附加到 URL 字符串中

First, what is the difference between POST and GET? Through experimenting, I know that GET appends the returning variables and their values to the URL string

website.com/directory/index.php?name=YourName&bday=YourBday

但是 POST 没有.

那么,这是唯一的区别,还是有特定的规则或约定来使用其中之一?

So, is this the only difference or are there specific rules or conventions for using one or the other?

其次,我还看到了 PHP 之外的 POSTGET:也在 AJAX 和 jQuery 中.POSTGET 在这三者之间有何不同?它们是相同的想法、相同的功能,只是使用方式不同吗?

Second, I've also seen POST and GET outside of PHP: also in AJAX and jQuery. How do POST and GET differ between these 3? Are they the same idea, same functionality, just utilized differently?

推荐答案

GETPOST 是两种不同类型的 HTTP 请求.

GET and POST are two different types of HTTP requests.

根据维基百科:

GET 请求指定资源的表示.请注意,GET 不应用于导致副作用的操作,例如将其用于在 Web 应用程序中执行操作.原因之一是 GET 可能会被机器人或爬虫任意使用,不需要考虑请求应该引起的副作用.

GET requests a representation of the specified resource. Note that GET should not be used for operations that cause side-effects, such as using it for taking actions in web applications. One reason for this is that GET may be used arbitrarily by robots or crawlers, which should not need to consider the side effects that a request should cause.

POST 将要处理的数据(例如,从 HTML 表单)提交到标识的资源.数据包含在请求正文中.这可能会导致创建新资源或更新现有资源或两者兼而有之.

POST submits data to be processed (e.g., from an HTML form) to the identified resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both.

所以本质上GET用于检索远程数据,而POST用于插入/更新远程数据.

So essentially GET is used to retrieve remote data, and POST is used to insert/update remote data.


HTTP/1.1 规范 (RFC 2616) 第 9 节 方法定义 包含有关 GETPOST 以及其他 HTTP 方法的更多信息,如果您有兴趣的话.


HTTP/1.1 specification (RFC 2616) section 9 Method Definitions contains more information on GET and POST as well as the other HTTP methods, if you are interested.

除了解释每种方法的预期用途外,规范还至少提供了一个为什么 GET 应该只用于检索数据的实际原因:

In addition to explaining the intended uses of each method, the spec also provides at least one practical reason for why GET should only be used to retrieve data:

使用 HTTP 协议的服务的作者不应该使用基于 GET 的表单来提交敏感数据,因为这会导致这些数据被编码到请求 URI 中.许多现有的服务器、代理和用户代理会在第三方可能看到的某个地方记录请求 URI.服务器可以使用基于 POST 的表单提交来代替

Authors of services which use the HTTP protocol SHOULD NOT use GET based forms for the submission of sensitive data, because this will cause this data to be encoded in the Request-URI. Many existing servers, proxies, and user agents will log the request URI in some place where it might be visible to third parties. Servers can use POST-based form submission instead


最后,对 AJAX 请求使用 GET 时的一个重要考虑因素是某些浏览器 - 特别是 IE - 将缓存 GET 请求的结果.因此,例如,如果您使用相同的 GET 请求进行轮询,您将始终获得相同的结果,即使您查询的数据正在服务器端更新.缓解此问题的一种方法是通过附加时间戳使每个请求的 URL 唯一.


Finally, an important consideration when using GET for AJAX requests is that some browsers - IE in particular - will cache the results of a GET request. So if you, for example, poll using the same GET request you will always get back the same results, even if the data you are querying is being updated server-side. One way to alleviate this problem is to make the URL unique for each request by appending a timestamp.

这篇关于POST 和 GET 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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