I just created an Angular app using the angular/cli and following their tutorial. Then I decided to include http get request using observables and a real rest API and noticed that angular was always getting null. I tried with several public rest APIs (they do return json objects when I put the URL in a browser) however when I console log the object returned it is always null. This is the last code of my service using a public API:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { catchError, map, tap } from 'rxjs/operators';
@Injectable()
export class myService {
private apiURL= 'https://jsonplaceholder.typicode.com/users';
constructor(private http: HttpClient) { }
getUsers() {
console.log('calling get!');
this.http.get('https://jsonplaceholder.typicode.com/users').pipe(
tap(users => console.log(users))).subscribe(p => {console.log(p);});
console.log('calling get finish!');
}
}
Any ideas?
Thanks everyone for the answers! The issue seems to be related to the browser we were using to test the call (Chrome Canary v65.0.3309.2 with security disabled to allow cross origin requests). We changed the config to run the app in other browsers and the HttpClient is working fine now... -.-