Tutorial :POST versus Ajax call



Question:

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


Solution:1

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 (because the page hasn't changed); the back button won't work as expected. 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 foreseeable future.


Solution:2

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


Solution:3

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


Solution:4

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.


Solution:5

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?


Solution:6

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.


Solution:7

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »