0

I am currently learning angular2 to create a web page. how can I connect to server with the link taomi.softape.io/api/item/item (provided by postman), I want to fetch data form the link.
here is my code

    var headers = new Headers();
    headers.append('Content-Type', 'application/json');
    this.http.post('taomi.softape.io/api/item/item', 
                   JSON.stringify({firstName:'Joe',lastName:'Smith'}),
                   {headers:headers})
   .map((res: Response) => res.json())
   .subscribe((res:Person) => this.postResponse = res);
Filburt
  • 17,626
  • 12
  • 64
  • 115
Kevin
  • 1
  • 2

1 Answers1

0

If you are fetching data, you should be using this.http.get, not post. Here is an example:

private _productUrl = 'api/products/products.json';

constructor(private _http: Http) { }

getProducts(): Observable<IProduct[]> {
    return this._http.get(this._productUrl)
        .map((response: Response) => <IProduct[]> response.json())
        .do(data => console.log('All: ' +  JSON.stringify(data)))
        .catch(this.handleError);
}
DeborahK
  • 57,520
  • 12
  • 104
  • 129
  • thanks Deborahk, they use post method to make a API call on the server and get the data, I confuse how can I fetch data if I use post method. thanks – Kevin May 27 '16 at 22:28
  • You could try: => this.postResponse = res.data); You could also try adding the .do. It is a debug statement that will show you what you are getting back (if anything). – DeborahK May 27 '16 at 22:44