I'm using Angular2 to create a simple web application. This application must call an API to get some datas.
I created a Service and a Component, as seen in the official tutorial.
The service:
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class WeatherService {
private url : string = 'http://127.0.0.1:1337/weather';
constructor(private http: Http) {
console.log('Weather URL API:', this.url);
}
public getWeather() {
return this.http
.get(this.url)
.toPromise()
.then(
(response) => {
console.log('response:',response);
},
(error) => {
console.log('Error:',error);
}
);
}
}
The problem is that this service always return an error:
Error: Object { _body: error, status: 0, ok: false, statusText: "", headers: Object, type: 3, url: null }
But in the Mozilla Firefox development tools, the API is called and return JSON with status code 200.
Maybe I made a mistake, but I don't see what and where. An idea ?
import 'rxjs/Rx'import 'rxjs/Rx'; import 'rxjs/add/operator/toPromise';?