Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular HttpClient get method not calling the server [duplicate]

Tags:

http

angular

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.

like image 576
Kingamere Avatar asked May 30 '18 16:05

Kingamere


People also ask

What is the use of HttpClient get () method?

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.

How do you perform error handling for HttpClient Angular?

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.

Why is HttpClient return observable?

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.


1 Answers

You must subscribe for the http response callServer().subscribe(x=> console.log(x)).

like image 128
Християн Христов Avatar answered Oct 11 '22 13:10

Християн Христов