Angular 2 http get not getting

后端 未结 4 1594
心在旅途
心在旅途 2020-11-22 06:22

I new to Angular 2 still learning I am trying to hit a URL with a get call but the get doesn\'t seem to go through even in browser\'s network I cannot find that get URL bein

4条回答
  •  感情败类
    2020-11-22 06:49

    Http uses rxjs and is a cold/lazy observable, meaning that you should subscribe to it to make it work.

    this.http.get(`http://swapi.co/api/people/1`)
      .map((response: Response) => {
        console.log(response.json());
        response.json();
      })
      .subscribe();
    

    Or if you want to subscribe from somewhere else, you should return the http.get method like this:

    getAllPersons(): Observable  {
      console.log("Here");
      return this.http.get(`http://swapi.co/api/people/1`)
        .map((response: Response) => {
          console.log(response.json());
          return response.json();
        });
    }
    

    and then :

    getAllPersons().subscribe();
    

提交回复
热议问题