防止 IE11 在 Angular 2 中缓存 GET 调用 [英] Prevent IE11 caching GET call in Angular 2

查看:34
本文介绍了防止 IE11 在 Angular 2 中缓存 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).

推荐答案

今天,我也遇到了这个问题,(该死的 IE).在我的项目中,我使用 httpclient,它没有 BaseRequestOptions.我们应该使用Http_Interceptor来解决它!

Today, I also had this problem, (damn IE). In my project, I use httpclient, that hasn't BaseRequestOptions. We should use Http_Interceptor to resolve it!

import { HttpHandler,
    HttpProgressEvent,
    HttpInterceptor,
    HttpSentEvent,
    HttpHeaderResponse,
    HttpUserEvent,
    HttpRequest,
    HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

export class CustomHttpInterceptorService implements HttpInterceptor {
    intercept(req: HttpRequest<any>, next: HttpHandler):
      Observable<HttpSentEvent | HttpHeaderResponse | HttpProgressEvent | HttpResponse<any> | HttpUserEvent<any>> {
      const nextReq = req.clone({
        headers: req.headers.set('Cache-Control', 'no-cache')
          .set('Pragma', 'no-cache')
          .set('Expires', 'Sat, 01 Jan 2000 00:00:00 GMT')
          .set('If-Modified-Since', '0')
      });

      return next.handle(nextReq);
  }
}

模块提供

@NgModule({
    ...
    providers: [
        ...
        { provide: HTTP_INTERCEPTORS, useClass: CustomHttpInterceptorService, multi: true }
    ]
})

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆