Angular http get not working and no error thrown

谁说我不能喝 提交于 2020-07-10 17:29:05

问题


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.

here is my service file code

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

Here is my component file code which calls the service

    evaluation(id: string) {
        this.searchService.findVariantById(id)
            .subscribe(
            evaluations => this.evaluations = evaluations,
            error => this.errorMessage = <any>error);
    }        

and Model

  export interface Evaluation {
    QuestionId: string;
    QuestionText: string;
    questionResponse: Array<Object>
  }

I have included appropriate dependencies for the same

   import { Observable } from 'rxjs/Observable';
   import 'rxjs/add/operator/map';
   import 'rxjs/add/operator/catch';
    import 'rxjs/add/operator/toPromise';

I am using "@angular/core": "~2.2.1", and "rxjs": "5.0.0-beta.12". Any kind of help is highly appreciated. Thank you

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

回答1:


This error was due to a Fake Backend Provider which Mocks HTTP requests.

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

By removing its dependencies from app.module.ts,it solved my problem.



来源:https://stackoverflow.com/questions/42829919/angular-http-get-not-working-and-no-error-thrown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!