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


当前回答

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

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

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

其他回答

meta http-equiv="Cache-Control" content="no-cache"

我把这个添加到视图中,它就开始在IE上工作了。确认可以在Angular 2上运行。

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

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

你可以添加一个拦截器。

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行。

在另一个线程复制我的答案。

对于Angular 2和更新版本,最简单的方法是重写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'
    });
}

并在你的模块中引用它:

@NgModule({
    ...
    providers: [
        ...
        { provide: RequestOptions, useClass: CustomRequestOptions }
    ]
})

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

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