使用Observable在Angular 2中发出轮询请求
问题描述:
我有以下代码在AngularJS 2中发出轮询GET请求:
I have the following code to make a polling GET request in AngularJS 2:
makeHtpGetRequest(){
let url ="http://bento/supervisor/info";
return Observable.interval(2000)
.map(res => res.json()) //Error here
.switchMap(() => this.http.get(url));
/*
This portion works well
return this.http.get(url)
.map(res =>res.json());*/
}
TypeScript给我一个错误,使响应以JSON格式显示(请检查代码中的注释.
TypeScript is giving me an error make the response in JSON format (check comment in the code.
TypeError: res.json is not a function
令人惊讶的是,它已经工作了一段时间,我不确定是否要更改其他任何内容,但是它停止了工作.
Surprisingly, it was working for some time and I am not sure if I changed anything else, but it stopped working.
代码中的注释部分效果很好.
The commented part in the code works very well.
答
尝试
return Observable.interval(2000)
.switchMap(() => this.http.get(url))
.map(res:Response => res.json());