2

IE is heavily caching xhr requests. To overcome this problem users suggested to add a random number to the url. For example here

Whereas this will work, I'm looking for a way to add a random number globally / disable IE caching globally, and caching has also be disabled for resource xhr get calls. I'm guessing that I could achieve this goal by using one of the following approaches. But I'm not sure what I exactly have to do..

a) Using $httpProvider.defaults.transformRequest

b) by using request/response promise chaining which have been added in v 1.1.4

4

2 回答 2

0

我通过使用 Django 和自定义中间件在服务器的响应中添加无缓存标头解决了缓存问题。

class NeverCacheXhrMiddleware(object):
    """
    sets no-cache headers for all xhr requests
    xhr requests must send the HTTP_X_REQUESTED_WITH header to 
    be identified correctly as a xhr request using .is_ajax()
    see: http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers
    """
    def process_response(self, request, response):
        if request.is_ajax():
            response['Cache-Control'] = 'no-cache, no-store, must-revalidate'
            response['Pragma'] = 'no-cache'
            response['Expires'] = '0'
        return response
于 2013-04-26T15:27:13.330 回答
0

这是 IE 的一个老问题。不知道如何使用 Angular 的 $httpProvider (或者我想要的),但这里有一个函数可以向任何 url 添加随机值,无论它是否已经使用查询字符串:

function noCacheUrl(url) {
    var urlParser = document.createElement('a');
    urlParser.href = url;
    var q = "nc" + Math.random() + "=1";
    q = urlParser.search ? urlParser.search + "&" + q : "?" + q;
    return urlParser.protocol + "//" + urlParser.host + urlParser.pathname + urlParser.hash + q;
}

请注意,添加的查询参数的名称是随机的,而不是值,因此不太可能与任何现有的名称/值对发生冲突。示例:&nc0.8578296909108758=1

http://jsfiddle.net/LgjLe/

于 2013-04-21T21:35:20.863 回答