How to stop IE from caching AJAX requests
by Justin Silverton
While working on an AJAX project over the weekend, I ran into the following issue: (through a GET request), every time I tried to call a certain function, It was returning the same data (which was supposed to be different each time)
I first tried the following (which should disable browser caching):
(in PHP)
header( “Expires: Mon, 26 Jul 1997 05:00:00 GMT” );
header( “Last-Modified: ” . gmdate( “D, d M Y H:i:s” ) . ” GMT” );
header( “Cache-Control: no-cache, must-revalidate” );
header( “Pragma: no-cache” );
The data still did not change.
I finally came to the following solutions:
1) use a POST request. When using with xmlhttprequest, it is slightly more complicated.
2) add a unique identifier to the end of my GET url.
I choose #2. A unique Identifier can be created using the current data+time. Here is a simple way to generate this (in Javascript):
var date = new Date();
var timestamp = date.getTime();
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleMessages;
xmlHttp.open(”GET”,”script.php?time=”+timestamp,true);
xmlHttp.send(null);
23 Comments so far
Leave a reply






Been using the header approach for a while w/o any problems. How thoroughly did you test?
Thanks a lot for this hint! I encountered exact this problem this morning & was just going to hunt for a solution.
You can combine both =)
“Been using the header approach for a while w/o any problems. How thoroughly did you test?”
I tried it with IE 5,6, and 7. 5 and 6 it worked most of the time and 7 not at all.
Yes this is a problem with IE.
Through serverside insertion of time, this can be done easily-
xmlHttp.open(”GET”,”script.php?time=”,true)
Thanks for the valuable hint. It’s good.
Hi, This is really helpful. I used this technique while I was facing problems with IE. And yes I am done. Thanks a lot. Keep up your great works.
Regards.
Rupom
http://www.rupom.info
still can’t cure the cache problem though, even if I use both techniques. My script works fine in firefox, but remains static in IE
POSTS are not cached in any browser. You may want to just pass your data that way if you are still having caching issues.
One another method is using Math.rand() * 10000;
:-)
the msdn site sez this is due to pre-check/post-check local cache behavior built into ie. i added the following response header and it worked:
Cache-Control: post-check=1,pre-check=2
check out the details here:
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/perf/perftips.asp
I have been using the unique identifier approach successfuly for quite awhile, but it always botherd me that those who poke around IE cache can see the the url requests with all the data in it. Converting to POST or encrypting data would be a solution, but it felt like overkill. The following took care of it in my case:
header(”Cache-Control: post-check=1,pre-check=2″);
header(”Cache-Control: no-cache, must-revalidate”);
header(”Pragma: no-cache”);
Thanks for the hints.
Thanks Justin. It works great. I have apply this to load xml using JavaScript for my SharePoint website.
Load XML using JavaScript in SharePoint here: http://www.digitalsaigon.com/Lists/Tech%20Tricks%20Tips%20Code%20Snippet/DispForm.aspx?ID=23
the best way to do this is
——————————————————–
After the open for your “GET” and before the send method insert the following code:
req.setRequestHeader(”If-Modified-Since”, “Sat, 1 Jan 2000 00:00:00 GMT”);
req.setRequestHeader(”Cache-Control”, “no-cache”);
Of course, use yourclassname insted of “req”.
These headers will solve your problem for IE and Firefox.
—————————————————-
RAVI NAIR
RAVI,
Thanks for the tips!
Hi Justin,
Let me know if it works for you. It does for me.
“Let me know if it works for you. It does for me”
It worked for me.
Hi Justin,
Thanks a lot, its working !!
Hi Guys,
I did try all methods listed above, and still in IE the content is being cached? Have these methods been tested on IE7?
In my case page keeps shuffling thumbnails at specific interval for which I have used xmlhttp method which works fine in Firefox but couldn’t work that successfully in IE (means it changes images only once thats it, after that changes happened with same data basically cache problem).
I tried both methods but unfortunately not fruitfull.
Anyway thanx man.
Thanks for the solution, Justin, approach #1 i.e header is working fine in IE, (checked in IE 7)
Tested several methods to solve this problem for IE7 and the solution of RAVI NAIR works like a charm! Many thanks! All the other methods found on the web did not work for me! Thanks again!
WOW!! What great trick!!!
I uses both of them but finally i preferred to use RAVI’s solution. Because it seems redundant when I’m sending time over GET variable and apply header on that page. Anyways Justin’s solution was rocking!!!
It saves lot of my effort! I uses this technique on http://www.StockBangladesh.com.
Thanks