How to Prevent Caching in IE8 when using AngularJS Models

橙三吉。 提交于 2019-12-03 17:34:38

You can set

"No-Cache headers to the response on server side"

var AppInfrastructure = angular.module('App.Infrastructure', []);

and in Angularjs you can write interceptor below is the sample code:

AppInfrastructure
    .config(function ($httpProvider) {
        $httpProvider.requestInterceptors.push('httpRequestInterceptorCacheBuster');
    })    
    .factory('httpRequestInterceptorCacheBuster', function () {
        return function (promise) {
            return promise.then(function (request) {
                if (request.method === 'GET') {
                    var sep = request.url.indexOf('?') === -1 ? '?' : '&';
                    request.url = request.url + sep + 'cacheSlayer=' + new Date().getTime();
                }

                return request;
            });
        };
    });   

same as what Jquery Guru mentioned above, but it is config in newer version of angular

.factory('noCacheInterceptor', function () {
        return {
            request: function (config) {
                console.log(config.method);
                console.log(config.url);
                if(config.method=='GET'){
                    var separator = config.url.indexOf('?') === -1 ? '?' : '&';
                    config.url = config.url+separator+'noCache=' + new Date().getTime();
                }
                console.log(config.method);
                console.log(config.url);
                return config;
           }
       };
});

you should remove console.logs after verifying

@Avinash seems you also only used the idea but not the exact solution, wasn't sure what you meant until i tried it, and had to do different implementation myself to make it work.

Anyways I decided to share my findings with the rest of the world so maybe to save someone else some time.

I tried to implement the above code 1 for 1... the first obvious issue I ran into is that the request parameter passed into the promise has an object structure inside that is different than what is shown above, to get the .method and .url i needed to first go into .config. So it was request.config.method.

But that wasn't the main problem, my problem was that even after implementing this the final url called did not have this cacheSlayer appended to it.

My solution :) when creating a $resource you pass in (url, additional settings, method). Inside the additional settings I passed in {'cacheSlayer':new Date().getTime()} and this way it did start adding this to my resource url's.

Hope this helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!