所有从IE发送的ajax调用都被Angular缓存了,我对所有后续调用都得到了一个304响应。虽然请求是一样的,但在我的情况下,响应是不一样的。我想禁用这个缓存。我尝试将缓存属性添加到$http。得到了,但还是没有用。如何解决这个问题?


当前回答

这有点过时了,但是:解决方案已经过时了。让服务器处理缓存或不缓存(在响应中)。保证没有缓存的唯一方法(考虑生产中的新版本)是用版本号更改js或css文件。我用webpack做这个。

其他回答

这个问题是由于你说的IE缓存问题,你可以在IE调试模式下按f12测试它(这在调试模式下工作很好)。IE不会在每次页面调用时获取服务器数据,而是从缓存中获取数据。要禁用此功能,请执行以下任一操作:

在下面附加HTTP服务请求url

//之前(已发行)

this.httpService.get(this.serviceUrl + “/eAMobileService.svc/ValidateEngagmentName/” + engagementName , {})

//After (working fine)

这httpService得到(this。“服务+”/移动服务。svc/验证名称/ + engagementName + ?日期=“+新日期”。

禁用整个模块的缓存:-

$httpProvider.defaults.headers.common['Pragma'] = '无缓存';

上面的解决方案是可行的(通过在查询字符串中添加一个新的参数使url唯一),但我更喜欢解决方案建议[这里]:更好的方法防止IE缓存在AngularJS?,它在服务器级处理这个问题,因为它不是特定于IE。我的意思是,如果资源不应该被缓存,那就在服务器上做(这与使用的浏览器无关;这是资源的固有属性)。

例如,在使用JAX-RS的java中,以编程方式为JAX-RS v1或以声明方式为JAX-RS v2执行此操作。

我相信任何人都能弄清楚怎么做

你可以添加一个拦截器。

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;
               }
           };
    });

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

你也可以尝试在你的服务中设置标题,例如:

...
import { Injectable } from "@angular/core";
import { HttpClient, HttpHeaders, HttpParams } from "@angular/common/http";
...
 @Injectable()
export class MyService {

    private headers: HttpHeaders;


    constructor(private http: HttpClient..) 
    {


        this.headers = new HttpHeaders()
                    .append("Content-Type", "application/json")
                    .append("Accept", "application/json")
                    .append("LanguageCulture", this.headersLanguage)
                    .append("Cache-Control", "no-cache")
                    .append("Pragma", "no-cache")                   
    }
}
....

正确的服务器端解决方案:在AngularJS中防止IE缓存的更好方法?

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "None")]
public ActionResult Get()
{
    // return your response
}