Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not always use HTTP post for ajax calls?

I know the differences between HTTP get and post methods (as expalined in big details in this question).
My question is why not always use the post method for AJAX calls, which is safe. Is there get request faster? is there is reason to prefer get then post?

For none ajax call there is a reason - to share a link to the same url, but for AJAX this argument it's not good...

like image 866
gdoron is supporting Monica Avatar asked Dec 13 '11 09:12

gdoron is supporting Monica


4 Answers

GET requests are smaller and faster; and leverage caching, both on the client side and on the part of any proxies that may be in play.

For data that isn't expected to change very often, GET requests are often very sensible, as they have a greater chance of not being resent unless necessary.

For data that is expected to change more often, however, POST is indeed the safer option, as it will always be resent to the server, making sure that changes are always respected.

There is also semantic issues that come into play. POST requests should really only be used when the intent is to modify data on the server.

like image 186
Williham Totland Avatar answered Sep 20 '22 14:09

Williham Totland


I was reading the Yahoo best practices for speeding up your websites some day back and they have very well explained why we should prefer get over post and here is the post snippet for your reference

using XMLHttpRequest, POST is implemented in the browsers as a two-step process: sending the headers first, then sending data. So it's best to use GET, which only takes one TCP packet to send (unless you have a lot of cookies). The maximum URL length in IE is 2K, so if you send more than 2K data you might not be able to use GET.

An interesting side affect is that POST without actually posting any data behaves like GET. Based on the HTTP specs, GET is meant for retrieving information, so it makes sense (semantically) to use GET when you're only requesting data, as opposed to sending data to be stored server-side.

For detail refer there page for details

Yahoo Best Practices for speed up

like image 24
Umesh Awasthi Avatar answered Sep 19 '22 14:09

Umesh Awasthi


My question is why not always use the post method for AJAX calls

Because in a RESTful application it wouldn't make sense to use the POST verb for actions that do not modify state on the server. In a RESTful application it doesn't really matter how the request was made: whether it was a normal, AJAX, or a robot.

Also GET requests are usually faster and are cached by browsers.

like image 34
Darin Dimitrov Avatar answered Sep 22 '22 14:09

Darin Dimitrov


GET uses a single request to the server vs. two for POST.

According to Yahoo's YSlow team you should opt for GET when the content transmitted is less than IE's 2K limit. Read more here: http://developer.yahoo.com/performance/rules.html#ajax_get

like image 35
Kenneth Benjamin Avatar answered Sep 21 '22 14:09

Kenneth Benjamin