All the ajax calls that are sent from the IE are cached by Angular and I get a 304 response
for all the subsequent calls. Although the request is the same, the response is not going be the same in my case. I want to disable this cache. I tried adding the cache attribute
to $http.get but still it didn't help. How can this issue be resolved?
16 Answers
Instead of disabling caching for each single GET-request, I disable it globally in the $httpProvider:
myModule.config(['$httpProvider', function($httpProvider) {
//initialize get if not there
if (!$httpProvider.defaults.headers.get) {
$httpProvider.defaults.headers.get = {};
}
// Answer edited to include suggestions from comments
// because previous version of code introduced browser-related errors
//disable IE ajax request caching
$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';
// extra
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
}]);
-
78The `If-Modified-Since` header makes IIS+iisnode throw 400 Bad Request for every html file loaded through `ngInclude` and `ngView`. The following two headers fixed the issue for me though (I pulled them from Chrome, which didn't have the caching issue): `$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';` `$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';` – Langdon Feb 05 '14 at 14:16
-
Perfect. This worked where the appending a unique query string did not. For anyone reading this, try this out sooner rather than later and save yourself some time. – rg88 Feb 14 '14 at 15:58
-
4In my opinion this answer should be marked as the answer, while the solution provided by Martin does work, it's more of a hack than an actual fix. – Robba May 23 '14 at 10:10
-
4This worked for my local GET requests, but it caused the one CORS request I was making to start using the OPTIONS method instead of the GET method. The 3rd party server doesn't support the OPTIONS method, so my workaround is to use jQuery.get() to make that request and use $scope.apply() in the response handlers. – Ben Jun 10 '14 at 13:58
-
13The usage of `If-Modified-Since = "0"` header breaks Tomcat (Problem with parsing header date, as `0` is not valid value [RFC](http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1)). Fixed using value `Mon, 26 Jul 1997 05:00:00 GMT` instead. – lopisan Jun 23 '14 at 12:07
-
Following cnmuc 's answer causes BF cache issues for me. @Langdon 's solution did fix my problem. – mithun_daa Nov 06 '14 at 16:10
-
@Langdon I'd stick your answer in a proper answer box, because you deserve the points for that one – marksyzm Nov 21 '14 at 10:52
-
This header fixed a problem I had with github API, where requests for contents of a repo yielded a cached and obsolete result. – Capaj Feb 12 '15 at 18:22
-
it should be noted that this won't work for jsonp requests - http://stackoverflow.com/questions/19603757/how-to-change-the-headers-for-angularjs-http-jsonp – chrismarx Mar 27 '15 at 19:36
-
nor will it work if you're using this with requests that require CORS and you don't additionally add the Cache-Control and Pragma headers to the list of headers in the Access-Control-Allow-Headers – chrismarx Mar 30 '15 at 19:09
-
6I didn't use the "If-Modified-Since" header and it worked without that. Just the other two are necessary. – Michael Mahony Nov 17 '15 at 22:02
-
Will the "per-request" setting cache=true have precedence on this general setting? – mvermand Feb 16 '16 at 15:37
-
Does this also ignore Google Chrome's Cache for requests that should be cached? I want to cache certain requests but with this fix they get ignored on Google Chrome/Firefox. – Carlos Magno Rosa Oct 11 '16 at 15:45
-
1Can someone please explain how I can implement this code? What file does it go and what line number in the Angularjs v1.3.9 ? – Jonathan Scialpi Jan 12 '17 at 21:49
-
1@JonathanScialpi, it goes in your actual angular app, not the angular.js file. After typing the initial line that defines the angular.module, you then type in the next line the code above, making sure that myModule is renamed to whatever variable you initialized your app to..... Also, thanks a lot, you saved my life. I was having trouble with a caching issue myself – OzzyTheGiant Feb 20 '17 at 04:08
-
This though breaks caching in all other cases, which is a lot of baby thrown away with the bathwater. – Jon Hanna Mar 14 '17 at 16:15
-
Anyone having issues with the above solution in Firefox? Still seems to be caching requests when I hit the back button and return to a page. – Nick Aug 25 '17 at 21:27
-
does this work with POST as well or would I need to list that explicitly, or possibly just use headers.common instead of explicitly listing the verbs. – Dylan Hayes Oct 31 '17 at 20:43
-
@cnmuc:- Why this issue occurs with angular and ie ? – Abhishek Sharma Jan 10 '18 at 10:01
-
how do i refresh on button click without http get or post request? – Dev Doc Oct 08 '18 at 06:21
You can either append a unique querystring (I believe this is what jQuery does with the cache: false option) to the request.
$http({
url: '...',
params: { 'foobar': new Date().getTime() }
})
A perhaps better solution is if you have access to the server, then you can make sure that necessary headers are set to prevent caching. If you're using ASP.NET MVC
this answer might help.
-
2`$http.get(url+ "?"+new Date().toString())` is just another representation, without using parameter but adding it to query string. – Davut Gürbüz May 17 '16 at 07:19
you may add an interceptor .
myModule.config(['$httpProvider', function($httpProvider) {
$httpProvider.interceptors.push('noCacheInterceptor');
}]).factory('noCacheInterceptor', function () {
return {
request: function (config) {
console.log(config.method);
console.log(config.url);
if(config.method=='GET'){
var separator = config.url.indexOf('?') === -1 ? '?' : '&';
config.url = config.url+separator+'noCache=' + new Date().getTime();
}
console.log(config.method);
console.log(config.url);
return config;
}
};
});
you should remove console.log lines after verifying.

- 1,782
- 17
- 16
-
-
2I am having serious caching problems in IE, which leads to a blank page, because important parts didn't execute. Using the propsed interceptor solved this problem! +1 – raoulinski Mar 25 '15 at 11:31
-
I think this is the best approach, since it avoids issues with CORS and IE's behavior of triggering a preflight request if you add additional headers. This seems to be the safest method to not run into additional problems – chrismarx Mar 30 '15 at 19:10
-
I simply added three meta tags into index.html on angular project, and cache issue was solved on IE.
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="Sat, 01 Dec 2001 00:00:00 GMT">

- 353
- 5
- 12
-
2We already had those meta tags in our `index.html` when we noticed IE11 was caching AJAX requests :/ But configuring `$httpProvider` as shown in other answers worked fine. – walen Apr 17 '17 at 10:50
Duplicating my answer in another thread.
For Angular 2 and newer, the easiest way to add no-cache
headers by overriding RequestOptions
:
import { Injectable } from '@angular/core';
import { BaseRequestOptions, Headers } from '@angular/http';
@Injectable()
export class CustomRequestOptions extends BaseRequestOptions {
headers = new Headers({
'Cache-Control': 'no-cache',
'Pragma': 'no-cache',
'Expires': 'Sat, 01 Jan 2000 00:00:00 GMT'
});
}
And reference it in your module:
@NgModule({
...
providers: [
...
{ provide: RequestOptions, useClass: CustomRequestOptions }
]
})

- 10,157
- 3
- 61
- 54
-
Wouldn't those be headers for the server response, not for the browser request? (I could imagine one could set `If-Modified-Since` with some date far in the past using the above method.) – Arjan Sep 07 '17 at 09:12
-
-
Your approach will remove any custom headers that are already there. Hence do the following instead of creating a new Header object. `headers: req.headers .set('Cache-Control', 'no-cache') .set('Pragma', 'no-cache') .set('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT')` – Chamika Goonetilaka Nov 21 '18 at 05:55
The guaranteed one that I had working was something along these lines:
myModule.config(['$httpProvider', function($httpProvider) {
if (!$httpProvider.defaults.headers.common) {
$httpProvider.defaults.headers.common = {};
}
$httpProvider.defaults.headers.common["Cache-Control"] = "no-cache";
$httpProvider.defaults.headers.common.Pragma = "no-cache";
$httpProvider.defaults.headers.common["If-Modified-Since"] = "Mon, 26 Jul 1997 05:00:00 GMT";
}]);
I had to merge 2 of the above solutions in order to guarantee the correct usage for all methods, but you can replace common
with get
or other method i.e. put
, post
, delete
to make this work for different cases.

- 5,281
- 2
- 29
- 27
-
can you tell me where in the code you have added this to the angular.js file? what line #? – Jonathan Scialpi Jan 13 '17 at 13:47
-
@JonathanScialpi I updated it to show where I might put it. Where it is within the anonymous function shouldn't matter. – marksyzm Jan 13 '17 at 15:40
-
@marksyzm can u plzz tell me what is the meaning of this line `if (!$httpProvider.defaults.headers.get) { $httpProvider.defaults.headers.common = {}; }` – Monojit Sarkar Jun 14 '17 at 11:23
-
@MonojitSarkar Ah, that was supposed to be headers.common in the if statement, thanks for that pointer – marksyzm Jun 16 '17 at 12:37
-
1`["If-Modified-Since"] = "0"` is illegal and with generate a Bad Request on some back ends. it should be a date. – jenson-button-event Aug 16 '18 at 16:47
This only line helped me (Angular 1.4.8):
$httpProvider.defaults.headers.common['Pragma'] = 'no-cache';
UPD: The problem is IE11 does aggressive caching. When I was looking into Fiddler I noticed that in F12 mode requests are sending "Pragma=no-cache" and endpoint is requested every time I visit a page. But in normal mode endpoint was requested only once at the first time when I visited the page.

- 171
- 2
- 6
-
1Just FYI, this answer caused a CORS issue when requesting files from Azure's blob storage, difficult to track down but eventually figured out this was the cause. Removing the pragma header fixed my CORS issue (but re-instated the IE caching issue). – keithl8041 Dec 09 '15 at 16:07
To avoid caching, one option is to give different URL for the same resource or data. To generate different URL, you can add a random query string to the end of the URL. This technique works for JQuery, Angular or other type ajax requests.
myURL = myURL +"?random="+new Date().getTime();

- 13,618
- 3
- 69
- 61
I get it resolved appending datetime as an random number:
$http.get("/your_url?rnd="+new Date().getTime()).success(function(data, status, headers, config) {
console.log('your get response is new!!!');
});

- 4,585
- 1
- 23
- 21
Solution above will work (make the url unique by adding in the querystring a new param) but I prefer the solution propose [here]: Better Way to Prevent IE Cache in AngularJS?, which handle this at server level as it's not specific to IE. I mean, if that resource should not be cached, do it on the server (this as nothing to do with the browser used; it's intrisic to the resource).
For example in java with JAX-RS do it programatically for JAX-RS v1 or declativly for JAX-RS v2.
I'm sure anyone will figure out how to do it
-
1Although it can be elaborated, this is the proper way of doing it. The client side should not be choosing what to cache or not, but it should be the server who should tell the client what needs to be cached or not. – Archimedes Trajano Aug 25 '15 at 19:21
-
The correct, server-side, solution: Better Way to Prevent IE Cache in AngularJS?
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public ActionResult Get()
{
// return your response
}

- 25,246
- 15
- 42
- 71

- 495
- 8
- 21
This is a little bit to old but: Solutions like is obsolete. Let the server handle the cache or not cache (in the response). The only way to guarantee no caching (thinking about new versions in production) is to change the js or css file with a version number. I do this with webpack.

- 1,931
- 2
- 16
- 20
also you can try in your servce to set headers like for example:
... import { Injectable } from "@angular/core"; import { HttpClient, HttpHeaders, HttpParams } from "@angular/common/http"; ... @Injectable() export class MyService { private headers: HttpHeaders; constructor(private http: HttpClient..) { this.headers = new HttpHeaders() .append("Content-Type", "application/json") .append("Accept", "application/json") .append("LanguageCulture", this.headersLanguage) .append("Cache-Control", "no-cache") .append("Pragma", "no-cache") } } ....

- 719
- 6
- 14
This issue is due to the IE caching problem as you said, you can test it in IE debug mode by pressing f12 (this will work fine in debug mode).IE will not take the server data in each time the page calls, it takes the data from cache. To disable this do either of the following:
- append the following with your http service request url
//Before (issued one)
this.httpService.get(this.serviceUrl + "/eAMobileService.svc/ValidateEngagmentName/" + engagementName , {})
//After (working fine)
this.httpService.get(this.serviceUrl + "/eAMobileService.svc/ValidateEngagmentName/" + engagementName + "?DateTime=" + new Date().getTime() + '', { cache: false })
- disable the cache for the entire Module :-
$httpProvider.defaults.headers.common['Pragma'] = 'no-cache';
meta http-equiv="Cache-Control" content="no-cache"
I just added this to View and it started working on IE. Confirmed to work on Angular 2.

- 10,266
- 13
- 49
- 75

- 888
- 10
- 11