2016-08-24 90 views
2

我想在cookie中放置一個標記唯一的問題,並且在獲取API的響應之前創建cookie,我該怎麼做才能等待作出迴應,然後創建cookie?等待Api的反應 - Angular js 2

我的代碼

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); 
} 
    } 

回答

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 
    ); 
+0

** **認購僅用於POST請求權的方法?或者我可以用來獲取請求,然後使用**數據**來執行代碼。 – user26776

+0

您可以將其用於所有**獲得**,**發佈**,**將**和**刪除**。 – rashfmnb