我怎样才能获得存储的cookie

时间:2017-07-17 08:36:50

标签: angular2-routing

我使用angular2请帮助我如何获取存储的cookie和存储的用户名

  LoginDetails(username: string, password: string, grant_type: string): Observable<boolean> {
        let loginurl = EmployeeUrls.LoginUrl_Login;
        let jam = this.encodeParams({ username: username, password: password, grant_type: grant_type });
        let headers = new Headers({ 'Content-type': ' application/x-www-form-urlencoded' });
        let options = new RequestOptions({ headers: headers });
        return this._Http.post(loginurl, jam, options).map((response: Response) => {
            //let access_token = response.json() && response.json().access_token;
            let access_token = response.json() && response.json().access_token;
            if (access_token) {
                // set token property
                this.access_token = access_token;
                // store username and jwt token in local storage to keep user logged in between page refreshes
                localStorage.setItem('currentUser', JSON.stringify({ username: username,token: access_token }));
                debugger;
                // return true to indicate successful login
                return true;
            }
            else {
                return false;
            }
        }

        )
    }

我尝试访问我的令牌和我的cookie我在构造函数中编写了一些代码

constructor(private _Http: Http) {
        var currentUser = JSON.parse(localStorage.getItem('username'));
        this.access_token = currentUser && currentUser.access_token;
    }

但我无法获取信息请帮助我

0 个答案:

没有答案