I have tried the http Angular 2 and TypeScript example on https://angular.io/docs/ts/latest/tutorial/toh-pt6.html it works. https://embed.plnkr.co/?show=preview
Updated code to use external Web Api
private headers = new Headers({'Content-Type': 'application/json'});
// private heroesUrl = 'api/heroes'; // URL to web api
private heroesUrl = 'http://localhost/WebApi2/api/hero'; // URL to web api
constructor(private http: Http) { }
getHeroes(): Promise<Hero[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => response.json().data as Hero[])
.catch(this.handleError);
}
Now I want to update it to use an external Web Api2 and get the error below.
EXCEPTION: Uncaught (in promise): Response with status: 404 Not Found for URL: http://localhost/WebApi2/api/hero" An error occurred Object { _body: Object, status: 404, ok: false, statusText: "Not Found", headers: Object, type: null, url: "http://localhost/WebApi2/api/hero" }
I have looked at this solution but it does not work for me. Angular2 http get request results into 404
Error on import
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { JSONP_PROVIDERS } from '@angular/http';
Http/Http/node_modules/@angular/http/index"' has no exported member 'JSONP_PROVIDERS'.
Could anyone point me in the correct direction, example of calling Web Api2 from Angular2?