【发布时间】:2016-11-01 15:46:48
【问题描述】:
我正在尝试学习angular2(来自php脚本,这很困难^^),在“英雄之旅”中使用真正的rest api。 根据我的阅读,我认为这可能很简单......
我有一个使用 Express 构建的工作 api:
curl -XGET http://localhost:3001/heroes
[{"_id":"58185c8a8af4b512c51c0519","no":"1","name":"bahamut","__v":0,"updated_at":"2016-11-01T09:12:42.803Z"},{"_id":"58185ca78af4b512c51c051a","no":"2","name":"gatz","__v":0,"updated_at":"2016-11-01T09:13:11.063Z"},{"_id":"58185ec98af4b512c51c051b","no":"3","nam...
在 hero.service.ts 中,我可以得到数据:
getHeroes(): Promise<Hero[]> { // {{{
console.log('getheroes in service');
console.log( "%o", this.http.get(this.heroesUrl).toPromise());
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => response.json().data as Hero[])
.catch(this.handleError);
} // }}}
当我在原始“英雄之旅”上执行相同的 console.log 时,我有一个数据数组,而这里我有一个字符串...... 我想我必须在某个地方转换字符串,但无论我尝试了什么,它都不起作用。
(我也读过很多关于 Observable 的例子,但我也没有成功)
帮我解释一下如何... TIA
日本
【问题讨论】:
-
localhost:3001/heroes 响应的 contentType 是什么?
-
你考虑过 JSON.parse(
)
标签: javascript rest angular