21

I'm trying to disable the cache in my AngularJS app, but it isn't working with the following code:

$http.get("myurl",{cache:false})

When I use "myurl&random="+Math.random(), the cache is disabled; but, I'd like a different approach.

Sam
  • 20,096
  • 2
  • 45
  • 71
Potato
  • 491
  • 1
  • 4
  • 13
  • This is already answered [here](http://stackoverflow.com/questions/16098430/angular-ie-caching-issue-for-http). – user6408463 Jul 05 '16 at 06:27
  • Possible duplicate of [Angular IE Caching issue for $http](https://stackoverflow.com/questions/16098430/angular-ie-caching-issue-for-http) – Ocaso Protal May 23 '17 at 08:54

4 Answers4

41

This is already answered here.

Pasting code snippet from the link for your reference.

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';
}]);
Community
  • 1
  • 1
user6408463
  • 436
  • 5
  • 4
  • 1
    I recommend not using Pragma one. it's not a standard (apparently) and may cause problems with Cross Origin requests. Just finished a 2 hour debug session with friends to understand that was the cause of the problem. – guy mograbi Feb 07 '18 at 01:32
  • @guymograbi What do you recommend then? – Schoof Sep 14 '18 at 11:54
  • @Schoof does the problem still occur if you simply remove Pragma? – guy mograbi Sep 14 '18 at 17:41
  • on chrome back button I use to get json bcoz of the same URL, from the above code I solved it Thank you!! – Ranjith Jun 07 '19 at 11:15
13

Here is what I did, simply change it to

 $http.get("myurl",{headers:{'Cache-Control': 'no-cache'}})
karma
  • 903
  • 10
  • 26
0

try like this

  $state(' 'app.name', {
 url: '/name',
cache: false,
 controller:'MainCtrl',
 templateUrl:'templates/name.html'
 })
Nikhil Ghuse
  • 1,258
  • 14
  • 31
-1
myApp.config(function ($routeProvider) {
        $routeProvider.
            when('/', {controller: 'MyCtrl', templateUrl: '/eshop/myConfig'})
})

.controller('MyCtrl', function ($scope, $templateCache) {
    $templateCache.remove('/eshop/myConfig');
    // or
    $templateCache.removeAll();
});

i didn't test it.i found something in this url.Have a look at this Angularjs - how to clear $routeProvider's caches of templateUrl

Community
  • 1
  • 1
naresh vadlakonda
  • 407
  • 1
  • 4
  • 13