如何在Angular中读取Http响应头

时间:2018-10-09 10:48:12

标签: angular http http-headers

这是我的角度服务类代码。我在此类中调用了Restful Web Service,响应即将到来。响应标头具有JWT Web令牌,我想读取响应标头并在此authUser服务函数中获取JWT Web令牌。怎么做?

import { Injectable } from '@angular/core';
import {ResponseBeanModule} from '../../module/responsebean/responsebean.module';
import { Http, Headers, Response } from '@angular/http';


@Injectable({
  providedIn: 'root'
})
export class AuthenticationService {
  constructor(private http: Http) { this._prepare()}
  private userUrl = 'http://192.168.1.152:8080/api/user/';

  endpointUrl: String;
  responseBeanModule: ResponseBeanModule = new ResponseBeanModule();
  options: any;
  headers: any;

  _prepare () {
    this.endpointUrl = this.userUrl;
    this.headers = new Headers();
    this.headers.set('Content-Type', 'application/json');
    this.options = {
      headers: this.headers,
      observe: 'body'
    };
  }



  public authUser(loginBean): Promise<ResponseBeanModule> {
    return new Promise((resolve, reject) => {
      this.http.post(this.userUrl, loginBean, this.options).subscribe(
        (data) => {
         alert(JSON.stringify(data));
         this.responseBeanModule = JSON.parse(data['_body'])
          return resolve( this.responseBeanModule );
        },
        (err) => {
          return reject(err);
        }
      );
    });

  }

}

0 个答案:

没有答案