angular - 等待 Api 的响应 - Angular js 2

标签 angular typescript cookies

我想在 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/

相关文章:

typescript - 有没有办法提取扩展类型的类型?

javascript - 使用事件监听器调用服务

c# - 如何以编程方式测试 cookie?

cookies - 是否可以禁用 session 存储/本地存储并启用 Cookie?

html - 格式化数据 block

ios - Ionic 2 fileTransfer.download 在 iOS 上没有反应

node.js - 将模型实例传递给函数

java - 设置 Cookie 在 Spring web-mvc 4 中不起作用

angular - 我们什么时候需要在 Angular 中创建服务?

angular - 如何为 mat-step-header 绑定(bind)点击事件