【问题标题】:Angular2 - http.get not calling the webpiAngular2 - http.get 不调用 webpi
【发布时间】:2017-04-04 14:43:17
【问题描述】:

我不知道为什么这个命令可以正常运行,但是我在 Fiddler 中找不到任何调用日志...

let z = this.http.get('http://localhost:51158/api/User/TestIT?idUser=0')

代码进入这一步,但如果我尝试使用 fiddler 捕获所有 http 请求,我找不到任何调用...

你知道发生了什么吗?

谢谢

【问题讨论】:

    标签: angular get fiddler


    【解决方案1】:

    要发起请求并接收响应,您可以添加 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 Example

    注意:未经测试的代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 2018-01-27
      • 2016-12-10
      • 2017-03-21
      • 2017-11-24
      • 1970-01-01
      相关资源
      最近更新 更多