2017-10-04 64 views
0

我需要從我的響應頭獲取Set-Cookie。我的回答是這樣的: enter image description hereAngular 2如何訪問標題

我的代碼如下所示:

this.http.get(url, {headers: this.getHeaders(token)}) 
     .timeout(this.timeout - this.loginTimeout) 
     .subscribe(res => { 
      var payload = res.json(); 
      var headers = res.headers; 
      var setCookieHeader = headers.get('Set-Cookie'); 
      console.log(setCookieHeader); 

但setCookieHeader總是空。有沒有什麼方法可以訪問這個響應頭?感謝您的任何幫助。

回答

0

首先,我認爲你需要升級到最新的http,在與angular 4.3+未來通過import { HttpClientModule } from '@angular/common/http';imports: [ HttpClientModule ]@NgModule

那麼你可以參考https://angular.io/guide/http#reading-the-full-response

http 
    .get<MyJsonData>('/data.json', {observe: 'response'}) 
    .subscribe(resp => { 
    // Here, resp is of type HttpResponse<MyJsonData>. 
    // You can inspect its headers: 
    console.log(resp.headers.get('X-Custom-Header')); 
    // And access the body directly, which is typed as MyJsonData as requested. 
    console.log(resp.body.someField); 
    });