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
.
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
but POST
doesn't.
So, is this the only difference or are there specific rules or conventions for using one or the other?
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?
This question is related to
http
post
get
http-method
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.
The only "big" difference between POST & GET (when using them with AJAX) is since GET is URL provided, they are limited in ther length (since URL arent infinite in length).
POST and GET are two HTTP request methods. GET is usually intended to retrieve some data, and is expected to be idempotent (repeating the query does not have any side-effects) and can only send limited amounts of parameter data to the server. GET requests are often cached by default by some browsers if you are not careful.
POST is intended for changing the server state. It carries more data, and repeating the query is allowed (and often expected) to have side-effects such as creating two messages instead of one.
Whilst not a description of the differences, below are a couple of things to think about when choosing the correct method.
With POST you can also do multipart mime encoding which means you can attach files as well. Also if you are using post variables across navigation of pages, the user will get a warning asking if they want to resubmit the post parameter. Typically they look the same in an HTTP request, but you should just stick to POST if you need to "POST" something TO a server and "GET" if you need to GET something FROM a server as that's the way they were intended.
If you are working RESTfully, GET should be used for requests where you are only getting data, and POST should be used for requests where you are making something happen.
Some examples:
GET the page showing a particular SO question
POST a comment
Send a POST request by clicking the "Add to cart" button.
Source: Stackoverflow.com