views:

54

answers:

2

Hello,
Is it possible to determine if a response returned by $.ajax was served from the browser's cache or was fresh?

Thanks.

+5  A: 

No, that's not possible. What you can do is to create a new URL every time (eg: by appending a random string to the URL, which is ignored by the script on the server), or by setting the response's cache headers appropriately.

See: http://www.mnot.net/cache_docs/#CACHE-CONTROL

You might want to set max-age, no-store, no-cache, must-revalidate or a combination of the previous.

NullUserException
Also, the img tag has a `complete` property to indicate whether or not the browser has loaded a particular image. http://www.devguru.com/technologies/ecmascript/quickref/image.html
karim79
Thanks. I see now.
Nazaf
+1  A: 

You can use the following in jQuery to make sure it does not cache:

$.ajaxSetup({cache: false}});

Am not sure what you are looking to achieve.

If you really want to know if the data is stale, you can do something like:

  1. Make your server written return a unique string each and every time it is called
  2. Check if this string exists in your javascript temp variables
  3. If yes, its stale, if no, its new.
Alec Smart
I don't want to modify the request. I just want to know after receiving some response from server (not necessarily mine) using AJAX if the response was cached or fresh.
Nazaf
Anyway, Thanks!
Nazaf