我正在玩 angular 2,但发送 http get 请求时遇到问题。 我创建了这样的方法:
test(){
console.log("call test");
let header = new Headers();
header.append("authorization",'change9ziKuJH8wnVbNES3AMleYGPKzZ');
this._http.get('http://localhost:42055/api/Question',{headers:header}).do(res => console.log("Result: " + JSON.stringify(res)));
主要问题是,此 http 请求从未发送过。我查看了 Fiddler,没有向我的 localhost:42055 发出请求。
不幸的是 Angular 不显示任何错误,所以我不知道发生了什么。
最佳答案
Observables 是惰性的,因此您需要订阅它们才能实际执行相应的处理(在您的情况下是 HTTP 请求):
this._http.get('http://localhost:42055/api/Question',{headers:header})
.do(res => console.log("Result: " + JSON.stringify(res)))
.subscribe((res) => { // <-------------
// handle result
});
关于http.get 不要打电话,Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36849737/