Angular 2 HTTP GET响应在使用内存中web api时有所不同
我正在使用实例(Angular.io/resources/live examples/toh-6/ts/eplnkr.html)编写Angular 2教程HTTP(Angular.io/docs/ts/latest/Tutorial/toh-pt6.html),作为我调查的基础。Angular 2 HTTP GET响应在使用内存中web api时有所不同,http,angular,Http,Angular,我正在使用实例(Angular.io/resources/live examples/toh-6/ts/eplnkr.html)编写Angular 2教程HTTP(Angular.io/docs/ts/latest/Tutorial/toh-pt6.html),作为我调查的基础。 此示例使用Angular内存中web api(github.com/Angular/in-memory web api)来模拟web服务器 在文件hero.service.ts中,使用以下功能从服务器加载数据阵列:
此示例使用Angular内存中web api(github.com/Angular/in-memory web api)来模拟web服务器 在文件hero.service.ts中,使用以下功能从服务器加载数据阵列: 一切正常,但分析hero.service.ts中的响应对象会显示不同的结果。
现在,成员体不是对象,而是包含express服务器发送的数据的JSON字符串
我的问题。这种不同响应的原因是什么?检查两个请求的标题,我认为这是它们的原因。如果ng2应用程序中缺少
Content-Type
标题,请添加application/json
值以请求。在这两种情况下,标题内容类型都是application/json
。对于express server,很容易验证,对于InMemoryWebAPI模块,我在MemoryDBService(memory data.service.ts中的文件)的类中添加了responseInterceptor()方法,并查看responseOptions。
getHero(id: number): Promise<Hero> {
const url = `${this.heroesUrl}/${id}`;
return this.http.get(url)
.toPromise()
.then(response => response.json().data as Hero)
.catch(this.handleError);
}
const heroes = [
{id: 1, name: 'Mr. One'},
{id: 2, name: 'Mr. Two'},
{id: 3, name: 'Mr. Three'}
];
function sendDatabaseResponse (req, res, next) {
if (req.method === 'GET' && req.url === '/api/heroes') {
let response = { };
response.data = heroes;
res.json(response);
}
else
next();
}