无法在angular5中获取令牌

时间:2017-12-23 15:37:46

标签: typescript angular5

输入脚本代码

import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { CookieService } from 'ngx-cookie-service';
@Injectable()
export class AuthenticationService {
    public token: string;
    constructor(private http: Http, private cookieService: CookieService) {}
 login(Username: string, password: string): Observable<boolean> {
        return this.http.post('/log', JSON.stringify({ username: Username, password: password }))
            .map((response: Response) => {
                const token = response.json() && response.json().token;

如何在我的角度中获得该令牌 即使我试过**const token: string = this.cookieService.get('token');** 无法获得响应中的令牌

提前致谢

1 个答案:

答案 0 :(得分:0)

将响应转换为json格式,这样你就会得到令牌

 .map((response: Response) =>{
    let res = response.json();
    let token = res.token;