Angular 2 http get not getting

后端 未结 4 1633
心在旅途
心在旅途 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 07:04

    method should return the response of api call using Observable.

    service.cs

    import { Http, Jsonp, Response, Headers, RequestOptions } from '@angular/http';
    import { Injectable } from '@angular/core';
    import { Persons } from './mock-people';
    import { Person } from './person';
    import { Observable } from 'rxjs/Observable';
    import 'rxjs/add/observable/forkJoin';
    
    @Injectable()
    export class Service {
      constructor(private jsonp: Jsonp, private _http: Http) { }
    
      getAllPersons():Observable{
        console.log("Here");
    
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers, method: 'get' });
    
        return this._http.get('http://swapi.co/api/people/' + personId)
            .map((res:Response) => {
                return res.json();
            })
            .catch(this.handleError);          
    
        console.log("Comes here 2");
      }
    
      private handleError(error: Response) {
        console.error(error);
        return Observable.throw(error.json().error || ' error');
      }
    }
    

    options & headers are optional.

    Note: instead of () you can define your datatype or any other defined type in which you get data in your response.

    Thank You.

提交回复
热议问题