Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 fine on Chrome & Firefox. However, I figured out that IE caches the GET response, and after I do a POST, I need to call the same GET request and get the new updated response but IE caches it. I want to disable caching just for IE. I tried using a

'If-None-Match': '*'

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

like image 731
noah Avatar asked Sep 30 '15 19:09

noah


2 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";
like image 169
Mahesh Durairaj Avatar answered Oct 01 '22 22:10

Mahesh Durairaj


Using the above info I was able to get this working on a one-off request inside a controller instead of changing the 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
    });
like image 45
FiniteLooper Avatar answered Oct 02 '22 00:10

FiniteLooper