我想在 cookie 中放入一个 token ,这是唯一的问题,而且 cookie 是在获得 API 响应之前创建的,我如何才能等待响应然后创建 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
);
关于angular - 等待 Api 的响应 - Angular js 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39128322/