AJAX & IE Caching Issues
I've been working on a project that involves using the jQuery javascript library and the Ajax methods. I've been happily using the Ajax jQuery.get() method to handle simple calls, such as a link that allows a user to view a list of words based on their selection, getting the contents of a tab when the user selects a tab, and finally getting the current weather after the person enters a new zip code, which is then saved to the database.
For the last operation, I got the JavaScript working great on Firefox and I thought it was working in Internet Explorer. However, after some testing, I noticed that the same weather data was being returned, even after a new zip code on the opposite coast was selected. Mmmm, I was rather baffled, especially since I've been using this jQuery.get() on the other functions, and encountered no problems like this.
So, I started the Visual Studio 2005 debugger and viewed the page in Internet Explorer. I stepped through each line of code, so far so good. Then I pressed the edit weather preferences button, the form popped up, I changed the zip code, pressed the save button, and stepped through each line of code again. The weather data was updated to the database correctly. Next, the form closed and the Ajax jQuert.get() method was called. The debugger should have gone to the page for returning the weather data, but that did not occur. Instead, the data from the very first Ajax call was returned. That could only mean one thing, the infamous caching issue, which can be the bane of web developers and dynamic content.
I quickly queried Google for "jquery ie caching get" and the very first entry, Ajax IE Caching Issues, had the solution. It turns out that, according to a comment from that article, the http specifications says any agent in the chain can cache the GET request, which Internet Explorer tends to do.
The solution is very simple, either use a POST request instead or add a parameter to the querystring that changes. Some ideas could be adding a timestamp to the end of the url, adding a flag that changes each time the weather data is returned.
Here's the original code that caused IE to cache the response. Notice how the url is always "ajax/weather.aspx?action=get", which causes IE to cache the response.
$.get("ajax/weather.aspx", { action: "get" } , function(data) { getUsersWeatherCallBackHandler( data ); } );
This code uses a timestamp to make the url unique each time the function is called.
var tsTimeStamp= new Date().getTime();
$.get("ajax/weather.aspx", { action: "get", time: tsTimeStamp} , function(data) { getUsersWeatherCallBackHandler( data ); } );
This code simply uses a post request instead to get around the caching problem.
$.post("ajax/weather.aspx", { action: "get" } , function(data) { getUsersWeatherCallBackHandler( data ); } );



I've another solution. you may send the http header "If-Modified-Since " in your request. eg. setRequestHeader("If-Modified-Since","0")
Left by flyfish | Jun. 23, 2008 at 8:28pm
thanks. i was having the very same problem until I happily stumbled upon this page.
Left by ashario | Oct. 1, 2008 at 7:24pm
Ashario, I'm glad you stumbled upon this page. Flyfish thanks a lot for the tip.
Left by Ken at SC | Oct. 2, 2008 at 4:09pm
thanks!
Left by Antonio Pedone | Oct. 16, 2008 at 7:10am
Thanks for posting this. When googling, this came up and really helped.
Left by rickcr | May. 4, 2009 at 3:26pm
You're welcome rickcr, glad you were able to find this page.
Left by Ken at SC | May. 6, 2009 at 5:27pm
Cheers Ken, tried numerous combinations of get/load and random characters and was getting weird results (as you found) with a project I was working on. All sorted now. Thanks again. GS
Left by Gareth S | Jul. 8, 2009 at 1:37pm
Thanks Gareth, glad you came across our blog article.
Left by Ken at SC | Jul. 9, 2009 at 11:59am
Thanks a lot for this article, it helps me to resolve my problem :)
Left by kamalcom | Aug. 5, 2009 at 2:19am
Thank you kamalcom, I'm glad to hear that you found the AJAX & IE Caching Issues blog useful.
Left by Ken at SC | Aug. 17, 2009 at 11:13am
Thanks! this solved my problem.
Left by M Iqbal | Dec. 13, 2009 at 1:09am
Glad it helped
Left by Ken at SC | Dec. 28, 2009 at 12:08pm
I had a problem with the content refresh in our intranet, this solution helped me to resolve it. Thanks to you!!!
Left by Suren | Feb. 14, 2010 at 3:15am
Ran into this same problem, but I got around it by using $.ajax with the "cache: false" option:
$.ajax({
url: "http://foo.com/foo.php",
type: "GET",
cache: false,
success: function(data) {
alert(data);
}
});
Left by Scooter | Feb. 15, 2010 at 12:49pm
Alternatively, jQuery has internal controls for caching. You can pass the cache parameter to the $.ajax() function. Or if you still want to use $.get() and $.post(), you can use $.ajaxSetup({cache: false}).
http://api.jquery.com/jQuery.ajax/
Left by Paul at SC | Feb. 16, 2010 at 2:24pm
Finnaly, I have been trying everything to prevent ie from cacheing my form data in an ajax request get method.
setRequestHeader("If-Modified-Since","0")
works!
Left by John Wilson | Feb. 22, 2010 at 7:11am
Scooter,
Thank you for your solution it worked perfectly for me.
$.ajax({
type: "GET",
url: "/google.com",
dataType: "xml",
cache: false,
success: function(xml) {
Left by Casey Becking | Mar. 4, 2010 at 8:55am
Leave a Comment