1

I have an AJAX request that sends a GET:'getPendingList'. This request should return a JSON string indicating a list pending requests that need to be approved. I'm a little confused about whether I should be using a GET or POST here.

From this website:

  • GET requests can be cached
  • GET requests can remain in the browser history
  • GET requests can be bookmarked
  • GET requests can be distributed & shared
  • GET requests can be hacked (ask Jakob!)

So I'm thinking: I don't want the results of this GET to be cached because the pending list could change. On the other hand, using POST doesn't seem to make much sense either.

How should I think about GET and POST? I've been told that GET is the same as a 'read'; it doesn't (or shouldn't) change anything on the server side. This makes sense. What doesn't make sense is the caching part; it wouldn't work for me if someone else cached my GET request because I'm expecting the data to change.

Avery
  • 10,391
  • 15
  • 38
  • 53

3 Answers3

1

Yahoo's best practices might be worth reading over. They recommend using GET primarily for retrieving information and using POST for updating information. In a separate item, they also recommend that do you make AJAX requests cachable where it makes sense. Check it out, it's a good read.

spaaarky21
  • 5,662
  • 4
  • 46
  • 63
0

In short, GET requests should be idempodent. POST requests are not.

If you are altering state, use POST - otherwise use GET.

And don't forget, when talking about caching with GET/POST, that is browser-caching.

Nothing stopping you from caching the data server-side.

Also, in general - JSON calls should be POST (here's why)

RPM1984
  • 69,608
  • 55
  • 212
  • 331
  • Well I _don't_ want the GET request to be cached. Example: Say I have a resource, a.jpg. I do a GET on this. But then I change the content of it on the server; if the browser caches a.jpg, then the client has stale data. So, unfortunately, it sounds like POST is the best be here. See the first answer from here: http://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get – Avery Nov 26 '10 at 03:35
  • @Avry - yes, you can solve that by adding querystring params to the files (a known trick with CSS files). – RPM1984 Nov 26 '10 at 03:38
0

So, after some IRC'ing, it looks like the best way to do this is to use GET (in this particular instance), but to prevent caching. There are two ways to do this:

1) Append a random string to your GET request.

This seems like a hacky way to do this but it sounds like it might be the only solution for IE: Prevent browser caching of jQuery AJAX call result.

2) In your response from the server, set the headers to no-cache.

It's not clear what the definitive behavior is on this. Some folks (see the previous link) claim that IE doesn't respect the no-cache directives. Other folks seem to think that this works: Internet Explorer 7 Ajax links only load once.

Community
  • 1
  • 1
Avery
  • 10,391
  • 15
  • 38
  • 53