2017-02-17 217 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' –