My server API is up and running, but the Angular call HttpClient.get does not seem to be calling the server.
Here is the method I'm calling:
private url = 'http://localhost:7002/myproj/api/domain';
callServer (): Observable<string> {
console.log("in callServer()");
return this.http.get<string>(this.url);
}
And this is based on the tutorial sample code:
/** GET heroes from the server */
getHeroes (): Observable<Hero[]> {
return this.http.get<Hero[]>(this.heroesUrl)
}
1) I know my API server is up and running.
2) I know the URL is correct.
3) The server isn't even logging or responding to the request (so I know the request isn't reaching the server.
4) My method callServer()
is being called because the log message is being printed to the console.
Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.
The basic way to handle errors in Angular is to use Angular's HttpClient service along with RxJS operators throwError and catchError. The HTTP request is made, and it returns the data with a response if anything wrong happens then it returns an error object with an error status code.
HttpClient is a built-in service class available in the @angular/common/http package. It has multiple signature and return types for each request. It uses the RxJS observable-based APIs, which means it returns the observable and what we need to subscribe it.
You must subscribe for the http response callServer().subscribe(x=> console.log(x))
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With