禁用AngularJS $ http缓存

Luf*_*ing 18 caching http angularjs

我正在尝试在我的AngularJS应用程序中禁用缓存,但它不能使用以下代码:

$http.get("myurl",{cache:false})
Run Code Online (Sandbox Code Playgroud)

当我使用时"myurl&random="+Math.random(),缓存被禁用; 但是,我想要一个不同的方法.

小智 38

这已在这里得到解答.

粘贴链接中的代码段以供参考.

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';
}]);
Run Code Online (Sandbox Code Playgroud)


小智 10

这是我做的,只需将其更改为

 $http.get("myurl",{headers:{'Cache-Control': 'no-cache'}})
Run Code Online (Sandbox Code Playgroud)