$ http的Angular IE缓存问题

Rah*_*hul 250 javascript caching angularjs

从IE发送的所有ajax调用都由Angular缓存,我得到一个304 response用于所有后续调用.虽然请求是相同的,但在我的情况下,回复并不相同.我想禁用此缓存.我尝试添加cache attribute到$ http.get但仍然没有帮助.如何解决这个问题?

cnm*_*muc 436

我没有为每个单个GET请求禁用缓存,而是在$ httpProvider中全局禁用它:

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)

  • `If​​-Modified-Since`标题使得IIS + iisnode对通过`ngInclude`和`ngView`加载的每个html文件抛出400 Bad Request.以下两个标题为我解决了这个问题(我将它从Chrome中删除,没有缓存问题):`$ httpProvider.defaults.headers.get ['Cache-Control'] ='no-cache'; ``$ httpProvider.defaults.headers.get ['Pragma'] ='no-cache';` (78认同)
  • 使用`If-Modified-Since ="0"`标题会破坏Tomcat(解析标题日期的问题,因为`0`不是有效值[RFC](http://www.w3.org/Protocols/rfc2616/ RFC2616-sec3.html#sec3.3.1)).固定使用值`Mon,26 Jul 1997 05:00:00 GMT`. (13认同)
  • 我没有使用"If-Modified-Since"标题,它没有使用它.只有其他两个是必要的. (6认同)
  • 在我看来,这个答案应该被标记为答案,而马丁提供的解决方案确实有效,它更像是一个黑客而不是实际修复. (4认同)
  • 这适用于我的本地GET请求,但它导致我正在进行的一个CORS请求开始使用OPTIONS方法而不是GET方法.第三方服务器不支持OPTIONS方法,因此我的解决方法是使用jQuery.get()来生成该请求,并在响应处理程序中使用$ scope.apply(). (4认同)

Mar*_*tin 69

您可以向请求附加一个唯一的查询字符串(我相信这是jQuery对cache:false选项的作用).

$http({
    url: '...',
    params: { 'foobar': new Date().getTime() }
})
Run Code Online (Sandbox Code Playgroud)

一个更好的解决方案是,如果您有权访问服务器,那么您可以确保设置必要的标头以防止缓存.如果您使用ASP.NET MVC 此答案可能会有所帮助.

  • `$ http.get(url +"?"+ new Date().toString())`只是另一种表示形式,不使用参数,而是将其添加到查询字符串中. (2认同)

dil*_*aik 28

你可以添加一个拦截器.

myModule.config(['$httpProvider', function($httpProvider) {
 $httpProvider.interceptors.push('noCacheInterceptor');
}]).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;
               }
           };
    });
Run Code Online (Sandbox Code Playgroud)

你应该在验证后删除console.log行.

  • 我在IE中遇到严重的缓存问题,导致页面空白,因为重要的部分没有执行.使用推进的拦截器解决了这个问题!+1 (2认同)

Hir*_*iro 14

我只是在angular项目的index.html中添加了三个元标记,并在IE上解决了缓存问题.

<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="Sat, 01 Dec 2001 00:00:00 GMT">
Run Code Online (Sandbox Code Playgroud)

  • 当我们注意到IE11正在缓存AJAX请求时,我们已经在`index.html`中有了这些元标记:/但是如其他答案所示配置`$ httpProvider`工作正常. (2认同)

Vit*_*kov 14

在另一个帖子中复制我的答案.

对于Angular 2和更新版本,no-cache通过覆盖添加标头的最简单方法是RequestOptions:

import { Injectable } from '@angular/core';
import { BaseRequestOptions, Headers } from '@angular/http';

@Injectable()
export class CustomRequestOptions extends BaseRequestOptions {
    headers = new Headers({
        'Cache-Control': 'no-cache',
        'Pragma': 'no-cache',
        'Expires': 'Sat, 01 Jan 2000 00:00:00 GMT'
    });
}
Run Code Online (Sandbox Code Playgroud)

并在您的模块中引用它:

@NgModule({
    ...
    providers: [
        ...
        { provide: RequestOptions, useClass: CustomRequestOptions }
    ]
})
Run Code Online (Sandbox Code Playgroud)


mar*_*yzm 9

我工作过的保证是这样的:

myModule.config(['$httpProvider', function($httpProvider) {
    if (!$httpProvider.defaults.headers.common) {
        $httpProvider.defaults.headers.common = {};
    }
    $httpProvider.defaults.headers.common["Cache-Control"] = "no-cache";
    $httpProvider.defaults.headers.common.Pragma = "no-cache";
    $httpProvider.defaults.headers.common["If-Modified-Since"] = "Mon, 26 Jul 1997 05:00:00 GMT";
}]);
Run Code Online (Sandbox Code Playgroud)

我不得不合并上述解决方案2,以保证所有方法的正确用法,但你可以替换common使用get或其他方法,即put,post,delete使这项工作对于不同的情况.


yam*_*xim 8

这条线只对我有帮助(Angular 1.4.8):

$httpProvider.defaults.headers.common['Pragma'] = 'no-cache';
Run Code Online (Sandbox Code Playgroud)

UPD:问题是IE11做了积极的缓存.当我查看Fiddler时,我注意到在F12模式下,请求发送"Pragma = no-cache",并且每次访问页面时都会请求端点.但在正常模式下,我第一次访问页面时只请求了一次端点.


Raz*_*aul 7

为避免缓存,一种选择是为相同的资源或数据提供不同的URL.要生成不同的URL,您可以在URL的末尾添加随机查询字符串.此技术适用于JQuery,Angular或其他类型的ajax请求.

myURL = myURL +"?random="+new Date().getTime();
Run Code Online (Sandbox Code Playgroud)


khi*_*nil 6

我得到它解决附加日期时间作为随机数:

$http.get("/your_url?rnd="+new Date().getTime()).success(function(data, status, headers, config) {
    console.log('your get response is new!!!');
});
Run Code Online (Sandbox Code Playgroud)