【问题标题】:http.get don't make a call, Angular 2http.get 不要打电话,Angular 2
【发布时间】:2016-04-25 19:47:17
【问题描述】:

我正在玩 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 没有显示任何错误,所以我不知道发生了什么。

【问题讨论】:

    标签: http get angular


    【解决方案1】:

    Observables 是惰性的,因此您需要订阅它们以实际执行相应的处理(在您的情况下为 HTTP 请求):

    this._http.get('http://localhost:42055/api/Question',{headers:header})
       .do(res => console.log("Result: " + JSON.stringify(res)))
       .subscribe((res) => { // <-------------
         // handle result
       });
    

    【讨论】:

      猜你喜欢
      • 2016-11-23
      • 1970-01-01
      • 2016-06-29
      • 2017-09-09
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-20
      相关资源
      最近更新 更多