What is the difference between POST and GET?
This question already has an answer here:
GET
and POST
are two different types of HTTP requests.
According to Wikipedia:
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.
and
POST submits data to be processed (eg, 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.
So essentially GET
is used to retrieve remote data, and POST
is used to insert/update remote data.
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. 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:
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
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. A POST
, unlike a GET
, typically has relevant information in the body of the request. (A GET
should not have a body, so aside from cookies, the only place to pass info is in the URL.) Besides keeping the URL relatively cleaner, POST
also lets you send much more information (as URLs are limited in length, for all practical purposes), and lets you send just about any type of data (file upload forms, for example, can't use GET
-- they have to use POST
plus a special content type/encoding).
Aside from that, a POST
connotes that the request will change something, and shouldn't be redone willy-nilly. That's why you sometimes see your browser asking you if you want to resubmit form data when you hit the "back" button.
GET
, on the other hand, should be idempotent -- meaning you could do it a million times and the server will do the same thing (and show basically the same result) each and every time.
Learn underlaying HTTP Protocol
This is similar to driving a car. You buy yourself a car and go on the road, but you don't know any of the signs, lights or other rules you must obey. Obviously you're not able to drive even though you know how to manage a car. At least not safely. Not for yourself, neither for others.
You should learn a bit about HTTP protocol. GET/POST are not related (at least not directly) to PHP/AJAX/jQuery or similar. They use them because they are using HTTP protocol for communication. And there's much more to HTTP Protocol than just GET
and POST
.
Check out some of these and then search on your own as well:
上一篇: 自定义HTTP标头:命名约定
下一篇: POST和GET有什么区别?