Click here to Skip to main content
Click here to Skip to main content

Tagged as

jQuery AJAX Cache Problems on IE

, 25 Jul 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Handling different default AJAX caching definitions between browsers.

Internet Explorer (IE) has this huge need to be different. In my opinion, this have no explanation other than the need to screw some development time Smile | :) Here I'll talk about another simple example: AJAX cache.

No other browser, by default, caches AJAX requests... but IE does. So, when you make a second AJAX call with the exact same parameters, IE thinks it's smarter than others and returns the result it has in cache from the first request, and there's no way to make it refresh that cache, not F5, not Ctrl+F5, not even asking politely... nothing!

What we have to do is tell, right on the request, that we don't want the result to be cached. So on a jQuery AJAX request, we would do:

cache: false,
url: 'http://myurl',
data: {}

This will work for this particular request. If you want to expand the scope of this setting to all AJAX requests, you can set it on the global AJAX configuration:

$.ajaxSetup({ cache: false });

From now on, I'll be making this cache setting a "must" on every AJAX call.


Be aware of this...

The theory behind this is that IE uses cache for requests with the exact same signature. So if you change something on the request the cache won't be used.
In web there's no maginc, and this thing here is no exception. When a $.ajax have the cache set to false what it does is append a dummy parameter to the query string.
The name of the parameter is a single _ (underscore) and the value is a timestamp.
This way they are quite sure there's no parameter name collision and that the querystring is always unique.

This usually isn't a problem unless you actually already use a parameter _ on your application querystring or if you perform some kind of URL parameters validation uppon request and find an unexpected parameter sitting at the end Smile | :)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Switzerland Switzerland
Senior IT Consultant working in Switzerland as Senior Software Engineer.
Find more at on my blog.

Comments and Discussions

GeneralTrue, POSTs don't have cache problems, mostly because they d... PinmemberAlexCode15-Aug-11 9:28 
Generalyou request the url via http GET method. if you use the POST... Pinmembertobias42115-Aug-11 1:57 
Generalin my case, ie uses cache also for request with parameter. i... Pinmembermmaurox26-Jul-11 22:50 
QuestionCode sample ? PinmemberSorin Pienaru21-Jul-11 22:01 
AnswerRe: Code sample ? PinmemberAlexCode21-Jul-11 22:46 
Hi, sure.
I commonly use JQuery to perform my ajax request, and 'till I found this I used $.getJSON most of the times.
Because this doesn't allow disabling cache I switched to $.ajax on all GET requests.
I'm also using and the AJAX request points to an ASHX.
This has nothing to do with it because putting a breakpoint on the ASHX it doesn't even get fired. The request never leaves the browser.
It's also very important that the request URL doesn't change.
The request must be exactly the same. If anything changes on the request will be made properly.
So an example may be:
$.getJSON('xpto.ashx', { id=10 }, function(data){ alert(data); });
I've added a dummy argument 'id' to the url.
As I'm writing this I'm not sure if cache is applied to a request without extra arguments.
On server side try returning the time as you were... should work.
Even yesterday I had exactly the same problem on another application.
Hope this helps.
GeneralRe: Code sample ? PinmemberSorin Pienaru25-Jul-11 17:42 
GeneralRe: Code sample ? PinmemberAlexCode25-Jul-11 17:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 25 Jul 2011
Article Copyright 2011 by AlexCode
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid