Weird Thoughts From Eric's Head

Tags - Categories : All | AJAX | BUSINESS | PERSONAL | PROGRAMMING | BOOK REVIEW

Ajax: Caching Problem with Requests

Well I am back from getting married and I am done with writing Ajax in Action so I thought that I would fill in some spare time and talk about a simple fix for a common question I get via email and the forums almost daily.

The question: The Ajax request is grabbing the cached page from the previous request, how do I stop this from happening? Well I have seen some fancy approaches by people, but there is one easy solution that works: a random query string value appended to the request.

So how and why does this work. Well it simple terms, when the browser looks at the destination, if it has a match, then it uses the data it has. Now this is great for when you are going to a static website, but since we are looking for new data, it sucks. By appending a random value to our parameters that we are sending back, it forces the browser to say, "Stop the press! We got new info here! Lets go get the data!"

I use this on my projects since I know my users have a major history of caching pages, especially with my dynamic PDF reports. It is always safer to append a random item to the query then having to worry about cached information.

The random tidbit of information you will submit to the server can be a number (larger the better), random string, or a timestamp. I prefer using the timestamp!

Well I hope this solves the problem of cached responses with Ajax. Remember to add your random friend to your parameters!

Eric Pascarello


Eric, This is a very helpful tips. I have a ajax request to add an record. After the record have been added and submit the second ajax to retrieve a recordset including the new record, I don't get the new record back. I append a random number in my parameter and the problem is resolved. Thanks the tip.
Duh! Thanks a lot! ... a simple "+Date()" appended to the request URL and viola!
When using ajax in http POST request,the browser doesn't cache this reuqest.
Ariel,
That is true, but you really should not be using post when you are just fetching data. There is a good article on it here: http://www.w3.org/2001/tag/doc/whenToUseGet.html that explains when a get should be used verus a post.
Eric
Exactly what I needed - THANK YOU!!!
Hey guys, just wanted to add a way to ad this random string to the URL. When passing the url you can append the random function Math.random(); to the url string like this:

var urlVar = urlVar + '&' + Math.random();

This will solve the problem also.

Good Luck!
lostlinkpr

Great!!! I am serching for this solution for my problem. Thanks a lot.
I have same kind of problem with my POST request. I tried this but it won't work. can anybody help me on this.
Thanks a ton for this.
A much more elegant solution would be to use the following code: xmlhttp.open("GET", theURL ,true); xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); xmlhttp.send(); The second line of the code instructs the XMLHttp object to avoid caching content that has been modified after the mentioned date. It worked fine for me...
Tank you Georgios Markakis your solution werked fine. i just added 1 line of code and the problem was fixed.
In the code: xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); Is this date format generic, regardless of the server's locale?
Yes I guess this is the standard date format included in all HTTP header requests. Since the time zone is included in the request (Greenwich Mean Time GMT) the server should be able to resolve this even if it belongs to another locale. I am using another locale than GMT and never experieneced any problems...
I had this problem of cached data when i implemented ajax. i thought there was no work around and i'd even suggested users to clean cache data from the browser ... but now, i think this simple yet effective solution can be implemented without much change :o) Thanks Eric ... and i must buy a copy of your book now :o)
Sounds good, but how do you avoid filling up the browser cache with responses? I've tried setting the following HTTP response headers: Expires: -1 Cache-Control: max-age=-1, must-revalidate, no-store, no-cache, post-check=-1, pre-check=-1 Pragma: no-cache, no-store But FireFox and IE both eat up a bit of in-memory cache space against my will. Any suggestions?
congrats on the marriage, this was exactly what i was looking for. Even funnier was that i've already bought your book, and then just googled a javascript question and got this page...it's always cool to see stuff after you've read the book!
I'm having the same problem using the prototype library, all that is updated with the ajax call is the filename of an image. This image needs to refresh even if the name is the same. The only way the cache is updated is to use a diff image name, any ideas?
Add a querystring to the image name when you change the source. Eric
I used the code given below and its working. if (req) { if (url.indexOf("?") != -1) { url = url + "&date=" + new Date(); } else { url = url + "?date=" + new Date(); } req.open('GET', url, false); That was very samll tip but very very useful. Thanks
Web change URL is good solution. It's basic and quicly
A great kiss for solving my problem.
I want my browser to load the image (which has same file name, as some image manipulation is happening) every time. But most of the time it is picking the cached image. I tried appending the querystring to the image source as you told, but of no use. I'm using IE 6. I set the browser caching to minimum also. Please suggest.
Absolute life saver. thanks
Thanks for the great tip. I spent a few hours trying to handle caching, and this was just a two liner to handle with your technique. Genius.


Add a comment

Title
Body
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
Math Quiz 8 + 4 = (Helps stop blog spam)
Name
E-mail address
Website
Remember me Yes  No 

E-mail addresses are not publicly displayed, so please only leave your e-mail address if you would like to be notified when new comments are added to this blog entry (you can opt-out later).

TrackBack to http://radio.javaranch.com/pascarello/addTrackBack.action?entry=1129908221072