Angular 2如何访问标题

时间:2017-10-04 21:38:19

标签: javascript angular https header

我需要从响应标头中获取Set-Cookie。我的回答如下: enter image description here

我的代码如下所示:

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始终为null。有没有办法如何访问此响应标题?感谢您的任何帮助。

1 个答案:

答案 0 :(得分:0)

首先,我认为您需要在angular 4.3+

import { HttpClientModule } from '@angular/common/http';和imports: [ HttpClientModule ]之后升级到@NgModule的最新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); }); >

然后你可以参考https://angular.io/guide/http#reading-the-full-response

df['signal'] = df.A.groupby((df.A != df.B).cumsum()).transform('head', 1)

df
   A  B  signal
0  0  1       0
1  1  0       1
2  0  0       1
3  0  0       1
4  0  1       0
5  0  0       0
6  1  0       1
7  0  0       1
8  0  1       0
9  0  1       0