Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling AngularJS $http cache

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.

like image 939
Potato Avatar asked Jul 05 '16 06:07

Potato


2 Answers

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'; }]); 
like image 152
user6408463 Avatar answered Sep 20 '22 04:09

user6408463


Here is what I did, simply change it to

 $http.get("myurl",{headers:{'Cache-Control': 'no-cache'}}) 
like image 32
karma Avatar answered Sep 20 '22 04:09

karma