24

我有一個休息終端,它在GET調用上返回一個列表。我也有一個POST端點來添加新項目和一個DELETE刪除它們。這適用於Firefox和Chrome,並且POST和DELETE在IE11中工作。但是,IE11中的GET僅適用於頁面的初始加載。刷新返回緩存的數據。我在Angular 1中看到過有關這種行爲的帖子,但對Angular 2(發佈候選者1)沒有任何評論。在Angular 2中阻止IE11緩存GET調用

+3

如果您獲取API不指定任何緩存控制頭 - >這意味着響應是可緩存如果狀態爲200 OK。 – Loc

+3

另請參閱http://stackoverflow.com/questions/36500804/proper-way-to-prevent-angular2-http-request-caching-in-internet-explorer-ie,瞭解客戶端解決方法。 –

+0

@Loc我添加了Cache-Control值no-store和no-cache,並且仍然只在IE中獲得相同的結果。 – cmaynard

回答

17

作爲回答here,你可能只覆蓋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 } 
    ] 
}) 
+1

這是一個很好的解決方案,可以讓角度請求無緩存的所有請求,但我不希望所有請求的行爲,因爲一些請求可以緩存很好。我去設置適當的頭服務器端。無論如何,我寧願服務器擁有緩存智能。我可能對這個問題措辭不佳。 – cmaynard

+1

@cmaynard我一邊看着如何爲Andular設置全局緩存,一邊衝擊你的問題,所以從谷歌的角度來看,你的措辭是人們搜索尋找的東西的完美:) –

0

有點晚了,但我遇到了同樣的問題。對於Angular 4.X我寫了一個自定義的Http類來追加一個隨機數到最後以防止被IE緩存。它基於dimeros的第二個鏈接(What is httpinterceptor equivalent in angular2?)。 警告:不能保證100%無錯。

import { Injectable } from '@angular/core'; 
import { Observable } from 'rxjs/Observable'; 
import { Http, Response, XHRBackend, RequestOptions, RequestOptionsArgs, 
URLSearchParams } from '@angular/http'; 

@Injectable() 
export class NoCacheHttp extends Http { 
    constructor(backend: XHRBackend, options: RequestOptions) { 
     super(backend, options); 
    } 

    get(url: string, options?: RequestOptionsArgs): Observable<Response> { 
     //make options object if none. 
     if (!options) { 
      options = { params: new URLSearchParams() }; 
     } 
     //for each possible params type, append a random number to query to force no browser caching. 
     //if string 
     if (typeof options.params === 'string') { 
      let params = new URLSearchParams(options.params); 
      params.set("k", new Date().getTime().toString()); 
      options.params = params; 

     //if URLSearchParams 
     } else if (options.params instanceof URLSearchParams) { 
      let params = <URLSearchParams>options.params; 
      params.set("k", new Date().getTime().toString()); 

     //if plain object. 
     } else { 
      let params = options.params; 
      params["k"] = new Date().getTime().toString(); 
     } 
     return super.get(url, options); 
    } 
} 
+0

我過去曾使用過這種技術來'欺騙'緩存。我認爲這很有用,但通常更好地設置適當的標題。 – cmaynard

2

今天,我也有這個問題,(該死IE) 。 在我的項目中,我使用httpclient,那還沒有BaseRequestOptions。 我們應該使用Http_Interceptor來解決它!

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 } 
    ] 
}) 
+0

謝謝,這是Angular 5的正確答案。然而,既然我在原始問題中指定了Angular 2,我將留下那個標記爲正確的答案,有一個upvote! – cmaynard