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
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
var urlVar = urlVar + '&' + Math.random();
This will solve the problem also.Good Luck!
lostlinkpr