Angular IE Caching issue for $http

Angular IE Caching issue for $http

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 . Though the request is the same, the response is not gonna be the same in my case. I wanna disable this cache. I tried adding the cache attribute to $http.get but still it didnt help. How can this issue be resolved?


Solution 1:

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';

Solution 2:

You can either append a unique querystring (I believe this is what jQuery does with the cache: false option) to the request.

    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.

Related:  How to check if anonymous object has a method?

Solution 3:

you may add an interceptor .

myModule.config(['$httpProvider', function($httpProvider) {
}]).factory('noCacheInterceptor', function () {
            return {
                request: function (config) {
                        var separator = config.url.indexOf('?') === -1 ? '?' : '&';
                        config.url = config.url+separator+'noCache=' + new Date().getTime();
                    return config;

you should remove console.log lines after verifying.

Solution 4:

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">

Solution 5:

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';

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:

    providers: [
        { provide: RequestOptions, useClass: CustomRequestOptions }

Solution 6:

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.

Related:  How to create JSON string in JavaScript?