Django Rest angular2 http get

时间:2017-04-18 16:11:28

标签: angular django-rest-framework

使用angular2与django休息Api。我试图在下面的代码中使用http获取服务来检索帐户:

getAccountBackend(id: string) : Promise<any> { 
   const slash="/"
   const urlaccount = `${this.urlAccontBackend}${id}${slash}`;
   console.log('url du compte', urlaccount)
   return this.http.get(urlaccount)
      .toPromise()
      .then(response => {
         response.json().data
         console.log(response)}
      )
     .catch(this.handleError);
}

尝试在组件中解析此方法时:

getAccountById(){
  console.log("i will resolve the account");
  this.fcbAccSrv.getAccountBackend('1234896')
    .then(data=> {
      this.compte=data
      console.log("the account from the backend", this.compte);
   })
 .catch(error =>{ this.error = error;
   console.log('error to resolve account')});
}

我收到了http ok的回复,但组件中的帐户未定义: enter image description here

1 个答案:

答案 0 :(得分:3)

您错过return,而您的回复似乎没有data,所以

.then(response => { response.json().data })

应该是:

.then(response => { return response.json() })