How do I prevent a jQuery Ajax request from caching in Internet Explorer?
-
1Using POST instead of GET prevents caching. stackoverflow.com/questions/6216234/disable-ajax-caching– Prashant GuptaJul 18, 2013 at 18:44
-
8YSlow and Chrome dev tools will warn you if you use POST requests for AJAX - generally GET should be the preferred method unless you really need to POST.– Pawel KrakowiakNov 27, 2013 at 13:25
-
Here is Your answer : disable Cache in IE– S.Mohamed Mahdi Ahmadian zadehFeb 7, 2017 at 6:59
6 Answers
You can disable caching globally using $.ajaxSetup()
, for example:
$.ajaxSetup({ cache: false });
This appends a timestamp to the querystring when making the request. To turn cache off for a particular $.ajax()
call, set cache: false
on it locally, like this:
$.ajax({
cache: false,
//other options...
});
-
2Wish I would have seen this global setup item before. Just saved me from having to update dozens of individual calls. Nov 29, 2012 at 3:21
-
2I know this is an old answer, just wondering if this affects all ajax like calls (ajax, get and post) or only specific ajax calls?– LumpyJan 26, 2013 at 19:21
-
6@Lumpy, according to jQuery.ajax() documentation,
cache: false
will only work correctly with HEAD and GET requests. Also, if you setcache: false
in$.ajaxSetup
, according to jQuery.ajaxSetup() documentation, it will "set default values for future Ajax requests." So yes, it will disable cache for all future HEAD and GET AJAX requests. Jun 7, 2013 at 21:29 -
11A word of caution: this adds "?_=somenumber" to your URL. Make sure your back end can ignore the "_" in the URL's query parameters.– user3458Sep 26, 2013 at 19:34
-
4+1 for
cache: false
. Turning off caching globally seems like overkill, but having control per request is perfect. Thanks Apr 16, 2014 at 10:17
If you set unique parameters, then the cache does not work, for example:
$.ajax({
url : "my_url",
data : {
'uniq_param' : (new Date()).getTime(),
//other data
}});
-
13That is what the
cache
parameter already does behind the scenes. Mar 6, 2013 at 12:33 -
The ajaxSetup code above did not work for me, didn't spend much time on figuring out why. Global variables are bad, and sometimes you want to cache. This answer is best in my opinion. Jul 23, 2013 at 15:50
-
@bladefist you seem to have hit the same problem as me. Do you know if there are other options apart from changing data to make IE stop caching ? The reason i'm asking is because my requests actually don't make use of 'data' and all data goes alongwith the URL.– badriApr 12, 2019 at 13:12
-
1Actually, this is surely what cache should be doing behind the scenes as per this information from - api.jquery.com/jquery.ajax cache (default: true, false for dataType 'script' and 'jsonp') Type: Boolean If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters.– badriApr 12, 2019 at 14:04
-
@bladefist It clearly says that it is going to append a timestap to the GET params. so wonder why the code provided by Koss works for you and disabling local or global cache doesn't?– badriApr 12, 2019 at 14:08
Cache-Control: no-cache, no-store
These two header values can be combined to get the required effect on both IE and Firefox
-
12The problem is that IE doesn't always listen to this, unfortunately :) Nov 29, 2010 at 13:05
-
1@Nick, is it sure? i didn't test this Cache-Control. Is Jquery Ajax chache is work on all browser?– user407283Nov 29, 2010 at 13:19
-
7It should work on all browsers, yes...but sometimes IE just likes to cache the hell out of things Nov 29, 2010 at 13:20
-
@Nick - I set the ajax cache to false.But the ajax success message didn't get in IE and also in FF. Can you please verify my code? is my code is wrong ? Thanks var ajaxfile = base+"index.php/msc/popup_view/"+obj+"/"+id+"/"+no_tab; $.ajax({type: "GET",url: ajaxfile, //contentType: "application/json; charset=utf-8", cache: false, success: function(msg){ $("#popup").html(msg); } });– SABUNov 30, 2010 at 4:37
-
I've just confirmed that IE11 ignores this header. Chrome threw a CORS error as well.– LeninNov 7, 2018 at 15:20
Here is an answer proposal:
http://www.greenvilleweb.us/how-to-web-design/problem-with-ie-9-caching-ajax-get-request/
The idea is to add a parameter to your ajax query containing for example the current date an time, so the browser will not be able to cache it.
Have a look on the link, it is well explained.
-
13That is what the
cache
parameter already does behind the scenes. Mar 6, 2013 at 12:34
you can define it like this :
let table = $('.datatable-sales').DataTable({
processing: true,
responsive: true,
serverSide: true,
ajax: {
url: "<?php echo site_url("your url"); ?>",
cache: false,
type: "POST",
data: {
<?php echo your api; ?>,
}
}
or like this :
$.get({url: <?php echo json_encode(site_url('your api'))?>, cache: false})
hope it helps
This is an old post, but if IE is giving you trouble. Change your GET requests to POST and IE will no longer cache them.
I spent way too much time figuring this out the hard way. Hope it helps.
-
I don't think you should be misusing HTTP actions. Settings
cache:false
on a case-by-case basis is the way to go. May 25, 2017 at 22:43