从Angular 4中的Observable Response获取响应令牌

时间:2018-01-04 14:41:59

标签: angular

我正在尝试从角度为4的CustomHttp.ts中的Observable Response方法获取响应令牌。

CustomHttp.ts

export class CustomHttp extends Http {
        constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) 
         {
            defaultOptions.withCredentials=true;
            super(backend, defaultOptions);
        }

在CustomHttp.ts中获取方法

get(url: string, options?: RequestOptionsArgs): Observable<Response> {
    return this.res(super.get(appConfig.apiUrl + url, this.addJwt(options)).catch(this.handleError));
}

Res方法:

 private res(Response:any)
 {
    return Response;
 }

它不起作用。如何获得响应令牌

2 个答案:

答案 0 :(得分:1)

你要做的事情很明确,你如何努力实现它就像泥水一样清晰。

使用新的HttpClient

http
  .get<MyDataType>('myEndpoint', {observe: 'response'})
  .subscribe(resp => {
    // Here, resp is of type HttpResponse<MyDataType>.
    // You can inspect its headers:
    console.log(resp.headers.get('Authorization'));
    // And access the body directly, which is typed as MyDataType as requested.
    console.log(resp.body);
  });

答案 1 :(得分:0)

如果我理解正确,您希望在您的组件中获取authorization标头。如果是这样,您可以subscribeget方法并按如下方式检索响应标头。

get(<your url here for your backend>)
.map(res => console.log("Authorization: " + resp.headers.get('Authorization')