Angular HTTP不起作用并且没有引发错误 [英] Angular http get not working and no error thrown
问题描述
围绕此主题有很多疑问,但我找不到合适的解决方案.我在控制台上看不到任何错误,这就是为什么它非常令人困惑的原因.
There are lot questions around this topic but I could not find a suitable solution. I am not able to see any error on the console, which is why its very confusing.
这是我的服务文件代码
findVariantById(id: string):Observable<Evaluation[]> {
const endPoint = 'Evalution/Get'
let params = new URLSearchParams();
params.set('key', '1234'); //For example API key would come into picture
params.set('id', id);
params.set('format', 'json');
params.set('callback', 'JSONP_CALLBACK');
// this much code gets called but nothing on Network tab.
return this.http.get(this.apiUrl + endPoint, { search: params })
.map<Evaluation[]>(this.extractData)
.catch<Evaluation[]>(this.handleError);
}
这是我的调用服务的组件文件代码
evaluation(id: string) {
this.searchService.findVariantById(id)
.subscribe(
evaluations => this.evaluations = evaluations,
error => this.errorMessage = <any>error);
}
和型号
export interface Evaluation {
QuestionId: string;
QuestionText: string;
questionResponse: Array<Object>
}
我已经包括了相同的依赖项
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/toPromise';
我正在使用"@ angular/core":〜2.2.1"和"rxjs":"5.0.0-beta.12" . 任何帮助都将受到高度赞赏.谢谢
I am using "@angular/core": "~2.2.1", and "rxjs": "5.0.0-beta.12". Any kind of help is highly appreciated. Thank you
更新:切换到Angular:4.0.1后,Http.Get和Post给出了错误
Update: After switching to Angular:4.0.1, Http.Get and Post gave error as
提供的参数与呼叫目标的任何签名都不匹配.
Supplied parameters do not match any signature of call target.
因此,我进行了以下更改.而且效果很好.
So I made following changes. And it worked perfectly.
return this.http.get(this.apiUrl + endPoint, { search: params })
.map(this.extractData)
.catch(this.handleError);
推荐答案
此错误是由于伪造HTTP请求的虚假后端提供程序引起的.
This error was due to a Fake Backend Provider which Mocks HTTP requests.
我已按照以下教程使用Fake Backend Provider进行登录和注册.
有关更多信息: http://jasonwatmore.com/post/2016/09/29/angular-2-user-registration-and-login-example-tutorial
I have used Fake Backend Provider to perform Login and registration, as per following tutorial.
More info on: http://jasonwatmore.com/post/2016/09/29/angular-2-user-registration-and-login-example-tutorial
通过从app.module.ts
删除其依赖项,它解决了我的问题.
By removing its dependencies from app.module.ts
,it solved my problem.
这篇关于Angular HTTP不起作用并且没有引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!