Angular2 - http.get 不调用 webpi
Angular2 - http.get not calling the webpi
我不知道为什么这个命令可以正常运行,但我在 Fiddler 中找不到任何调用日志...
let z = this.http.get('http://localhost:51158/api/User/TestIT?idUser=0')
代码进入这一步,但如果我尝试使用 fiddler 捕获所有 http 请求,我找不到任何调用...
你知道发生了什么事吗?
谢谢
要发起请求并接收响应,您可以将 map()
和 .catch()
添加到 return 来自您的方法的 Observable 响应。
示例服务:
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
...
getMyData(): Observable<any> {
return this.http.get('http://localhost:51158/api/User/TestIT?idUser=0')
.map((res: Response) => {
console.log(res);
return res;
})
.catch((err) => {
// TODO: Error handling
console.log(err);
return err;
}
}
然后订阅Observable-returning方法执行请求:
示例订阅
...
this.getMyData()
.subscribe((res: any) => {
console.log(res);
},
error => {
// TODO: Error handling
console.log(error);
});
对于一个很好的入门示例,您可以参考 Angular Tour of Heroes Example
注:未经测试的代码
我不知道为什么这个命令可以正常运行,但我在 Fiddler 中找不到任何调用日志...
let z = this.http.get('http://localhost:51158/api/User/TestIT?idUser=0')
代码进入这一步,但如果我尝试使用 fiddler 捕获所有 http 请求,我找不到任何调用...
你知道发生了什么事吗?
谢谢
要发起请求并接收响应,您可以将 map()
和 .catch()
添加到 return 来自您的方法的 Observable 响应。
示例服务:
import { Http, Response } from '@angular/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
...
getMyData(): Observable<any> {
return this.http.get('http://localhost:51158/api/User/TestIT?idUser=0')
.map((res: Response) => {
console.log(res);
return res;
})
.catch((err) => {
// TODO: Error handling
console.log(err);
return err;
}
}
然后订阅Observable-returning方法执行请求:
示例订阅
...
this.getMyData()
.subscribe((res: any) => {
console.log(res);
},
error => {
// TODO: Error handling
console.log(error);
});
对于一个很好的入门示例,您可以参考 Angular Tour of Heroes Example
注:未经测试的代码