How to disable caching of AJAX requests in Angular in IE

I have an angular application that makes HTTP GET calls to the server. It works great on Chrome and Firefox. However, I realized that IE caches the GET response, and after doing the POST, I need to call the same GET request and get a new updated response, but IE caches it. I want to disable caching for IE only. I tried to use

'If-None-Match': '*'

the request header for my GET calls, but then this disables caching for everything. Is there a way to do this conditionally only for IE? Or is there another way to disable it?

+4
source share
3 answers

HTML

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

OR

Js

if (!$httpProvider.defaults.headers.get) {
        $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"] = "0";
+4
source

$http, IE. 200 304. .

$httpProvider.

angular.module(ApplicationConfiguration.applicationModuleName)
.config(['$httpProvider', function($httpProvider) {
    //initialize get if not there
    if (!$httpProvider.defaults.headers.get) {
        $httpProvider.defaults.headers.get = {};    
    }    

    //disable IE ajax request caching
    $httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon, 26 Jul 1997 05:00:00 GMT';

    $httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
    $httpProvider.defaults.headers.get['Pragma'] = 'no-cache';
}]);
+1

Using the above information, I managed to get this work to work with a one-time request inside the controller instead of changing global defaults:

var configOptions = {
    headers: {
        common: {
            "Cache-Control": "no-cache",
            "If-Modified-Since": "0",
            "Pragma": "no-cache"
        }
    }
};

$http.get("path/to/file.json", configOptions)
    .then(function (response){
        //do stuff
    });
+1
source

Source: https://habr.com/ru/post/1609692/


All Articles