Prevent IE11 caching GET call in Angular 2

前端 未结 7 567
甜味超标
甜味超标 2020-11-28 09:19

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

相关标签:
7条回答
  • 2020-11-28 09:43

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

    Module provide

    @NgModule({
        ...
        providers: [
            ...
            { provide: HTTP_INTERCEPTORS, useClass: CustomHttpInterceptorService, multi: true }
        ]
    })
    
    0 讨论(0)
提交回复
热议问题