使用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')});
}
答案 0 :(得分:3)
您错过return
,而您的回复似乎没有data
,所以
.then(response => { response.json().data })
应该是:
.then(response => { return response.json() })