等待Api的响应——Angular js 2



我想在cookie中放置一个令牌,唯一的问题是,在获得API的响应之前创建cookie,我如何才能等待响应,然后创建cookie?

我的代码
getlogin()
{
this._symfonyservice.login(this.user,this.pass).subscribe(//call the post
            data => this.results = data, // put the data returned from the server in our variable
            error => console.log("Error HTTP Post Service"), // in case of failure show this message
            () => console.log(this.results['token'])//run this code in all cases
        );
if(this.results['token']!="")
{
    let token = this.results['token'];
    this.deleteCookie("Cookieteste");
    this.setCookie("Cookieteste",token, 1);
}
   }

data代替() final function

getlogin()
{
this._symfonyservice.login(this.user,this.pass).subscribe(//call the post
data => {
            this.results = data
            if(this.results['token']!="")
            {
                let token = this.results['token'];
                this.deleteCookie("Cookieteste");
                this.setCookie("Cookieteste",token, 1);
            }       
        }, // put the data returned from the server in our variable
                error => console.log("Error HTTP Post Service"), // in case of failure show this message
                () => console.log('completed')//run this code in all cases
     );

相关内容

  • 没有找到相关文章