2中阻止IE11缓存GET调用

2中阻止IE11缓存GET调用

本文介绍了在Angular 2中阻止IE11缓存GET调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个休息端点,它在GET调用上返回一个列表。我还有一个POST端点来添加新项目,还有一个DELETE来删除它们。这适用于Firefox和Chrome,POST和DELETE适用于IE11。但是,IE11中的GET仅适用于页面的初始加载。刷新返回缓存的数据。我在Angular 1中看过关于此行为的帖子,但Angular 2(发布候选人1)没有任何内容。

I have a rest endpoint that returns a list on a GET call. I also have a POST endpoint to add new items and a DELETE to remove them. This works in Firefox and Chrome, and the POST and DELETE work in IE11. However, the GET in IE11 only works on initial load of the page. Refreshing returns cached data. I have seen post about this behavior in Angular 1 but nothing for Angular 2(release candidate 1).

推荐答案

对于 Angular 2和更新,通过覆盖 RequestOptions 添加无缓存标头的最简单方法:

For Angular 2 and newer, the easiest way to add no-cache headers by overriding 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 }
    ]
})

这篇关于在Angular 2中阻止IE11缓存GET调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:06