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


当前回答

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

...
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")                   
    }
}
....

其他回答

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

...
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")                   
    }
}
....

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

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

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

试试这个,在类似的情况下它对我很有效:-

$http.get("your api url", {
headers: {
    'If-Modified-Since': '0',
    "Pragma": "no-cache",
    "Expires": -1,
    "Cache-Control": "no-cache, no-store, must-revalidate"
 }
})

你可以添加一个唯一的querystring(我相信这就是jQuery对cache: false选项所做的)到请求。

$http({
    url: '...',
    params: { 'foobar': new Date().getTime() }
})

一个可能更好的解决方案是,如果您可以访问服务器,那么您可以确保设置必要的头文件以防止缓存。如果你使用ASP。这个答案可能会有帮助。

一种选择是使用简单的方法,为每个请求添加时间戳,不需要清除缓存。

    let c=new Date().getTime();
    $http.get('url?d='+c)