[英]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()
以從方法返回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示例
注意:未經測試的代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.