2017-06-28 15 views
1

私のチームはスプリングブートを使用してRESTサービスを開発しました。タスクは、ログインモジュールを構築することです。承認はバックエンドで行われます。トークンは、バックエンドでトークンの「Authorization」フィールドに提供されます。私はGoogleで検索しようとしています角度2のヘッダー応答からトークンを読み取る方法は?

authenticateUser(user: ILogin): Observable<Response> { 

    const url = `${this.baseUrl2}` 

    // console.log("the username after service is " + user.username + " and the password is " + user.password) 

    let headers = new Headers({ 'Content-Type': 'application/json' }); 
    let options = new RequestOptions({ headers: headers }); 

    console.log("the json format is " +JSON.stringify(user)); 

    return this.http.post(url, JSON.stringify(user)) 
        .map((response: Response) => { 

         let userJson = response.headers; 

         console.log("the token is" +userJson.token); // here I got a type error and the error is that there is no token as the field of headers. 


     }) 
        .do(data => console.log('authenticateuser:' + JSON.stringify(data))) 
        .catch(this.handleError); 

} 

と私はどのようにトークンを読み取るためにどのような方法を見つけられませんでした:

は、今私は私が持っていることはある角度の2

からこのトークンを読みたいです誰か助けてもらえますか?

PS:私は

let userJson = response.json().token 

let userJson = response.headers; 

から私のコードを変更したが、問題が戻ってトークンを終了するには、ヘッダーにしている場合に設置されていることである場合、私は、トークンを読むことができますI上記のように私のコードを変更すると、私は体にトークンを探していることを意味し、コードをコンパイルするとランタイムエラーが出ます。

+0

so ...ヘッダーとトークンを読み取ることができますか? – crash

答えて

1
authenticateUser(user: ILogin): Observable<Response> {  
    let headers = new Headers({ 'Content-Type': 'application/json' }); 
    let options = new RequestOptions({ headers: headers }); 

    console.log("the json format is " +JSON.stringify(user)); 

    return this.http.post(url, JSON.stringify(user)) 
        .do((data:response) => {console.log(data.headers.get('token'))})) 
        .map((response: Response) => <any> response.json()) 
        .catch(this.handleError); 
} 
+0

あなたのコードについて少し説明すればよいでしょう。 –

関連する問題