views:

414

answers:

7

Now that most modern browsers support AJAX and client-side requests without performing a POST, what is the role of POST (form post)?

Are there situations or general rules when a POST will always be preferred to a XmlHttpRequest?

All that POST is doing is placing variable key value pairs inside the server head. The advantages of POST I can think of are large amounts of data and mobile browsers. Are there many others I'm missing?

N.B. I know you can perform POSTs with AJAX calls, I'm talking primarily about with a <form> tag

+1  A: 

If you need to refresh the entire page, a post would probably be easier.

Kevin
+5  A: 

Some actions can't be carried out using AJAX requests due to security restrictions. For example, uploading files can't be done with AJAX.

Welbog
I didn't think about that, good point.
Kevin
jQuery will automatically internally create an iframe, and a form in the iframe, and POST that form, if it detects an file upload within the Ajax request.
James Curran
Even so, that's still a POST, which was my point.
Welbog
A: 

The key differentiator is that the POST of a form sends data and can navigate to a new URL in a single round trip to the server. Ajax can send data but can't navigate to new page.

AnthonyWJones
Couldn't you use window.location.href=xx after the call was successful?
Chris S
@Chris, that trigers a GET, which would be a second round-trip to the server.
James Curran
A: 

Er .. posting forms?

Post is really good for big forms as the values are hidden in the html headers and not tacked onto the url string?

James Anderson
You can use SOAP for Ajax though
Chris S
A: 

If theory, an HTTP GET should be an idempotent request for information: Two identical GETs should return the exact same information.

HTTP POSTs, on the other hand, are intended to hold state and perform actions: Two identical POSTs maybe perform different action depending on who doing it, what was done before, and when it is being done.

James Curran
+2  A: 

This question is a little tricky because it conflates two concepts. The first is POSTing and the second is Ajax. POSTing, as compared to GETting, is a different HTTP method with different implementation and semantics. Ajax, or XmlHttpRequest, has its counterpart in normal requesting/navigation. You can use POST or GET for both XmlHttpRequest and normal navigation and form submission (well, POST is always a "form submission"). The difference between XmlHttpRequest and normal requests is that a normal request replaces the page with a new page.

You could write just about any website using only Ajax to get data and change the DOM; that's mainly how Gmail works. There are no "form submissions" in the traditional sense. But there are still GETs and POSTs because the server and browsers interpret the results differently. GET is supposed to be idempotent; POST is meant for operations that change the state on the server. For example, an ecom transaction should be a POST. This doesn't change when using Ajax because you want proxy servers to also understand that this is a POST and that they shouldn't try to cache the response.

There are also advantages and disadvantages to using GET vs POST. You can't bookmark the results of a POST because the parameters are hidden. You can't GET something with parameter values of unlimited length because IE only supports about 2000 chars.

Also there are disadvantages to using Ajax vs normal submissions; you can't bookmark the resulting page; the back button won't work as expected. You can't upload a file using Ajax. But with Ajax you could minimize the data transfered and also provide convenient updates to a page (such as monitoring the status of a long process) without annoying flickering or refreshing.

In summary the two request types, Ajax and traditional form submission, can both be used with GETs and POSTs, and there are pros and cons for each. Neither type can do everything the other can and so you can expect to see a mix for the forseeable future.

Mr. Shiny and New
A: 

I'm going to answer my own question with this video I recently stumbled across by Doug Crockford. He describes the see-saw between all ajax and all page refreshes.

Chris S