【问题标题】:Django Rest angular2 http getDjango Rest angular2 http 获取
【发布时间】:2017-09-14 14:10:55
【问题描述】:

我将 angular2 与 django rest Api 一起使用。我正在尝试使用以下代码中的服务中的 http get 检索帐户:

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 的响应,但组件中的帐户未定义:

【问题讨论】:

    标签: angular django-rest-framework


    【解决方案1】:

    您缺少return,并且您的回复似乎没有data,所以

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

    应该是:

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

    【讨论】:

      猜你喜欢
      • 2016-11-28
      • 2017-01-31
      • 2017-01-18
      • 2016-07-28
      • 2017-10-07
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 2017-05-04
      相关资源
      最近更新 更多