Angular http无法正常工作,也没有抛出任何错误

时间:2017-03-16 09:25:26

标签: angular typescript angular-http

围绕这个主题有很多问题,但我找不到合适的解决方案。我无法在控制台上看到任何错误,这就是为什么它非常令人困惑。

这是我的服务文件代码

   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”。 任何形式的帮助都非常感谢。谢谢

更新:切换到Angular:4.0.1后,Http.Get和Post给出了错误

  

提供的参数与呼叫目标的任何签名都不匹配。

所以我做了以下更改。而且效果很好。

  return this.http.get(this.apiUrl + endPoint, { search: params })
        .map(this.extractData)
        .catch(this.handleError); 

1 个答案:

答案 0 :(得分:0)

此错误是由于Focks后端提供程序Mocks HTTP请求造成的。

我已按照以下教程使用Fake Backend Provider执行登录和注册 有关详细信息:http://jasonwatmore.com/post/2016/09/29/angular-2-user-registration-and-login-example-tutorial

通过从app.module.ts删除其依赖项,它解决了我的问题。