2017-02-17 215 views
2

我需要幫助,我試圖從響應中獲取cookie,但我找不到方法,我對tsc和ng2很新。Angular 2 - 從響應中獲取Cookie

這是NG2 HTTP POST

return this._http 
    .post('http://demo...', body, { headers: headers }) 
    .subscribe(
     (response: Response) => { 
      this.storeToken(response); 
     }, (err) => { 
      console.log('Error: ' + err); 
     } 
    ); 

這是服務器響應:

HTTP/1.1 200 OK 
Server: Apache-Coyote/1.1 
Access-Control-Allow-Origin: http://localhost:3000 
Access-Control-Allow-Credentials: true 
Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With 
Set-Cookie: JSESSIONID=A099CC4CA7A25DFBD12701630A7DC24C; Path=/pbcp/; HttpOnly 
Content-Type: application/json;charset=UTF-8 
Transfer-Encoding: chunked 
Date: Fri, 17 Feb 2017 04:08:15 GMT 

32 
{"status":"OK","message":"User is Authenticated."} 
0 

我很困惑,因爲我看不到它的頭陣...

結果console.log(response)

Response from Chrome console

結果的

console.log(response.headers)

Headers array from Chrome console

...,但我可以看到它在餅乾部分。

Cookies section in Chrome

謝謝!

+0

試試這個資源。get(「set-cookie」),並讓我知道 –

+0

也嘗試這個多個cookie let resHeader:Headers = res.headers; resHeader.getAll('set-cookie'); –

+0

嗨@Vinay,首先,感謝您的時間。您的建議不起作用,因爲響應中沒有包含該名稱的標題,請參閱我編輯的帖子。嘗試打印這個console.log(response); –

回答

10

那麼,經過一番研究,我發現了兩個問題。

第一個,cookie被設置爲OK,但我在錯誤的地方尋找它。所有這一次,我在我的域名localhost:3000下查找它,並將cookie正確存儲在http://demo...域下,這是正確的行爲。我可以看到它在chrome://settings/ ==>顯示高級設置==>所有Cookie和網站數據... ==>和過濾遠程主機類似以下內容:

enter image description here


,我忘記在請求標題的其餘部分使用withCredentials: true,以便自動包含和接受cookie。

authenticate(username: string, password: string) { 
    var body = `{"username":"${username}","password":"${password}"}`; 
    var headers = new Headers(); 
    headers.append('Content-Type', 'application/json'); 
    let options = new RequestOptions({ headers: headers, withCredentials: true }); 

    return this._http 
       .post('http://demo...', body, options) 
       .subscribe(
        (response: Response) => { 
        this.doSomething(response); 
       }, (err) => { 
        console.log('Error: ' + err); 
       }); 
} 

Thanks @All for your response and time!

+1

有了這個答案,你可以通過'response.headers'來訪問Set-Cookie頭嗎? – PhoneixS

+0

不需要訪問Set-Cookie,因爲它是爲每個請求自動添加的,所以只需設置'withCredentials:true',工作正常 –

0

我假設你正在使用angular2 http模塊聯繫服務器,它將返回一個可觀察到的服務器響應。

,如果你訂閱它,您可以使用響應:

//... 
http.post(...your content...) 
.take(1) // optional: depending on your needs you could .take(x) this to just take a certain number of responses and then terminate the subscription in order to not have a "hot Observable" lying around 
.subscribe(response =>{ 
     .console.log(response); 
    // if I am not mistaken "response.headers" should contain the "Set-Cookie" you are looking for 
}); 

你也可以轉變可觀察到一個承諾:

http.post(...your content...) 
    .toPromise() 
    .then(response=>{ 
    console.log(response); 
    let headers = response.headers; 
    console.log(headers); 
    }) 
    .catch(err=>console.log(err)); 

在一定的反應情況下,您可能需要使用其改造response.json()來檢索和對象。

希望這會有所幫助。如果這不是你要找的東西,給我一些更多的細節,我會盡力幫忙。

+0

嗨@jparg,感謝您的時間,我正在使用您發佈的帖子,但這不起作用,因爲當我檢查標題數組時,我看不到「set-cookie」。 ,看到我更新的帖子 –

+0

好吧,這很奇怪如果你嘗試獲取cookies:headers.getAll(「set cookie」)我假設你回到空數組然後呢? – jparg

+0

不,我得到'null' –