Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Ajax/javascript result caching in browsers?

How to prevent browsers from caching Ajax results? I have and event triggered Ajax script the displays results only when the browsers data has been cleared.

Tested in IE6 and Firefox 3.0.10

like image 668
Babiker Avatar asked Jun 04 '09 06:06

Babiker


3 Answers

The random URL works, but it's kind of a hack. HTTP has solutions built in that should work. Try using the solution indicated here. Basically, set the headers:

"Pragma":            "no-cache",
"Cache-Control":     "no-store, no-cache, must-revalidate, post-check=0, pre-check=0",
"Expires":           0,
"Last-Modified":     new Date(0), // January 1, 1970
"If-Modified-Since": new Date(0)
like image 69
Matthew Flaschen Avatar answered Oct 13 '22 00:10

Matthew Flaschen


Add a random query string to the URL you are sending.

E.g. if the Ajax request is sent to "http://www.xyz.com/a" then add a random string at the end: "http://www.xyz.com/a?q=39058459ieutm39"

like image 28
Itay Maman Avatar answered Oct 13 '22 00:10

Itay Maman


I've used the jQuery {cache: false} method and it worked like a charm.

The complete code example is like this:

$.ajaxSetup({cache: false});
like image 39
Mitch Labrador Avatar answered Oct 12 '22 23:10

Mitch Labrador