Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14532

Re: GET vs POST in AJAX

From Stefan Weiss <krewecherl@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: GET vs POST in AJAX
Date 2012-06-16 13:13 +0200
Organization albasani.net
Message-ID <jrhpod$g06$1@news.albasani.net> (permalink)
References <bcc85577-4540-4c88-8c4a-4fd9492c6f89@googlegroups.com>

Show all headers | View raw


On 2012-06-16 03:16, CODEY wrote:
> Hello, I have been using javascript for so many years, but I never
> really understood the difference between GET and POST ... obviously i
> understand the main idea, that GET is getting information and POST is
> sending, but since you can send and receive with POST, and because
> POST does not have the length/size restriction that GET does, I am
> not really sure why I would ever want to use GET instead of POST ...
> I am talking about ajax requests ....

As you say, the difference is mostly semantic. In a nutshell,

- GET retrieves information from the server. It should not have any
other side effects changing the state on the server, or at least the
client may assume that the request is "safe".

- POST sends data to the server. The client has to assumed that
successful sending can change the state of the server (by adding the
POSTed data to the server's data store, or causing other changes).

The W3 link Andrew Poulos posted explains the difference in detail. The
authoritative specifications are in RFC2616 for HTTP/1.1 and RFC1945 for
HTTP/1.0.

The short of it is, never use GET to add/change/delete information on
the server.

If the server provides a (partially) RESTful API, the distinction
between different request methods may be enforced by the server, and the
client may not have a choice which method to use.

In practice, with AJAX/XMLHttpRequest, another consideration is often
more important: POST responses cannot be cached on the client. This can
be (ab)used to prevent client side caching.

For example: Assuming that both server and client are configured to
allow caching, if the client sends a GET request via AJAX, it may
receive a response indicating that the content will not change for the
next 10 days. The next time the client uses GET for the same URI, the
request may not even be sent to the server, because the response is
already locally cached.

  Client: GET: show me the todo list for user X
  Server: ok, here's the todo list
  Client: POST: add entry to todo list for user X
  Server: ok, added
  Client: GET: show me the todo list for user X

If the results of the first GET were cached, this request may never be
sent at all. In a perfect world, the server will tell the client not to
cache the first response, and the client will respect that, but in
practice, all kinds of things can go wrong with caching. So people often
just use POST to prevent caching, even if it's semantically incorrect.

- stefan

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

GET vs POST in AJAX CODEY <ed.pataky@gmail.com> - 2012-06-15 18:16 -0700
  Re: GET vs POST in AJAX Andrew Poulos <ap_prog@hotmail.com> - 2012-06-16 13:31 +1000
    Re: GET vs POST in AJAX CODEY <ed.pataky@gmail.com> - 2012-06-21 15:43 -0700
  Re: GET vs POST in AJAX Jason C <jwcarlton@gmail.com> - 2012-06-15 21:14 -0700
  Re: GET vs POST in AJAX Stefan Weiss <krewecherl@gmail.com> - 2012-06-16 13:13 +0200
    Re: GET vs POST in AJAX Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-16 12:22 -0700
      Re: GET vs POST in AJAX Stefan Weiss <krewecherl@gmail.com> - 2012-06-16 21:58 +0200
        Re: GET vs POST in AJAX Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-16 13:26 -0700
          Re: GET vs POST in AJAX Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-17 09:06 +0200
            Re: GET vs POST in AJAX Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-06-17 20:57 -0700
              Re: GET vs POST in AJAX Stefan Weiss <krewecherl@gmail.com> - 2012-06-18 15:23 +0200
                Re: GET vs POST in AJAX Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-18 20:17 +0200
                Re: GET vs POST in AJAX Stefan Weiss <krewecherl@gmail.com> - 2012-06-19 03:41 +0200
                Re: GET vs POST in AJAX Andrew Poulos <ap_prog@hotmail.com> - 2012-06-19 12:25 +1000
                Re: GET vs POST in AJAX Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-19 19:19 +0200
                Re: GET vs POST in AJAX Dr J R Stockton <reply1225@merlyn.demon.co.uk.not.invalid> - 2012-06-19 20:44 +0100
              Re: GET vs POST in AJAX Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-18 20:14 +0200

csiph-web