Angular2 - http.get不会调用webpi [英] Angular2 - http.get not calling the webpi

查看:150
本文介绍了Angular2 - http.get不会调用webpi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 <$ c $我不知道为什么这个命令可以正常运行,但我无法在Fiddler中找到任何调用日志。 c> let z = this.http.get('http:// localhost:51158 / api / User / TestIT?idUser = 0')

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



你有什么想法吗?



谢谢

解决方案

要发起请求并接收到响应,您可以添加 map() .catch()返回可观察响应



示例服务

  import {Http,Response } from @ angular / http'; 
导入'rxjs / add / operator / catch';
导入'rxjs / add / operator / map';
...

getMyData():可观察< any> {
返回this.http.get(的 'http://本地主机:51158 /原料药/用户/的TestIt ID用户所= 0?')
.MAP((RES:响应)=> {
console.log(res);
return res;
})
.catch((err)=> {
// TODO:错误处理
console.log(err);
return err;
}
}



然后订阅Observable-returning方法来执行请求:

示例订阅

  ... 

this.getMyData()
.subscribe((res:any)=> {
console.log(res);
},
error => {
// TODO:错误处理
console.log(error);
});

对于一个好的初学者例子,你可以参考 Angular Heroes Tour 示例


$ b 注意:未经测试的代码 p>

I don't know why this command runs properly but I can't find any log of calls in Fiddler...

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

The code pass into this step but If I try to catch all the http request using fiddler, I can't find any call...

Do you have idea on what is happening ?

Thanks

解决方案

To initiate a request and receive a response you can add map() and .catch() to return an Observable response from your method.

Example Service:

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;
     }
}

Then subscribe to the Observable-returning method to execute the request:

Example Subscription

...

this.getMyData()
        .subscribe((res: any) => {
            console.log(res);
        },
        error => {
            // TODO: Error handling
            console.log(error);
        });

For a good starter example you can refer to the Angular Tour of Heroes Example

Note: Untested code

这篇关于Angular2 - http.get不会调用webpi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆