mororo
mororo

Reputation: 1134

Angular HTTP POST not firing

I'm having trouble while making a simple call from my NG app to my ASP.NET Core WebAPI. The code is the one following:

  this.http.post(actionUrl, credentialsDto)
  .map((data: Response) => {
    if (data !== null) {
      localStorage.setItem(this.tokenStorageKey, JSON.stringify(data));
    }
});

I tried to log my "data" callback parameter, but adding a call to console.log() in my if statement results in nothing (means i have no console output at all). Meanwhile if i add a console.log() call before or after the call, but in the same function, they are perfectly executed.

Does anyone know what's happening here? I'm a JS novice, so don't blame me. I'm using the HTTP client native in Angular 5.

Thanks in advance.

Upvotes: 1

Views: 2540

Answers (1)

Suren Srapyan
Suren Srapyan

Reputation: 68685

You need also to subscribe to the Observable. Observable will not fire if you haven't subscribed to it.

this.http.post(actionUrl, credentialsDto)
         .map((data: Response) => {
               if (data !== null) {
                  localStorage.setItem(this.tokenStorageKey, JSON.stringify(data));
               }

               return response;
          }).subscribe(response => /* something here */).

Starting from RxJS 5.5 you can't use map operator like you did. You need to use it inside pipe function.

RxJS 5.5 and higher approach

this.http.post(actionUrl, credentialsDto)
         .pipe(map((data: Response) => {
               if (data !== null) {
                  localStorage.setItem(this.tokenStorageKey, JSON.stringify(data));
               }

               return response;
          })).subscribe(response => /* something here */).

Upvotes: 8

Related Questions