【发布时间】:2017-02-17 05:05:49
【问题描述】:
我需要帮助,我正在尝试从响应中获取 cookie,但我找不到方法,我对 tsc 和 ng2 还很陌生。
这是 ng2 http 帖子
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
我很困惑,因为我在 headers 数组中看不到它...
console.log(response)的结果
console.log(response.headers)的结果
...,但我可以在 cookie 部分看到它。
谢谢!
【问题讨论】:
-
试试这个 res.get("set-cookie") 并告诉我
-
对于多个 cookie 也试试这个 let resHeader: Headers = res.headers; resHeader.getAll('set-cookie');
-
嗨@Vinay,首先,感谢您的宝贵时间。您的建议无效,因为响应没有具有该名称的标题,请参阅我编辑的帖子。
-
尝试打印这个console.log(response);而不是 console.log(response.headers) 这将打印来自服务器的标题。并确保您从服务器发送标头。
-
@Vinay 我已根据您的要求编辑了原始帖子,谢谢。
标签: angular typescript cookies