POST reply comes empty

0

service.ts:

teste(user) {    

    return this.http.post('api/home', user)
  }

teste.component.ts:

ok(user){           
    this.loginServ.teste(user)
      .subscribe(data => {
        let retorno = (data as any)
        console.log(retorno);
      })
  }

Return is only empty and I can not understand why. Can anyone help?

    
asked by anonymous 14.12.2018 / 19:53

2 answers

1

instead of:

teste(user) {    

    return this.http.post('api/home', user)
  }

I solved it like this:

teste(user) {    

    return this.http.post('api/home',  {user:user})
  }
    
17.12.2018 / 19:48
1

I believe you are returning a Promise , then the .then() method should be used in the request, the .subscribe() method is for Observables .

Service

function teste(user) {    

return this.http.post('api/home', user);

}

For Promises

ok(user){           
    this.loginServ.teste(user)
      .then(success);

    function success(response){
        console.log(response);
    }
}

For Observables

ok(user){           
    this.loginServ.teste(user)
      .subscribe(data => {
       console.log(data);
      });
  }

Promises vs Observables

Promises - Promises deal with sigular events when an asynchronous operation succeeds or fails.

Observable - An Observable is like a Stream and allows you to pass 0 or more events when the response is returned and also call functions or callbacks for those events. >     

14.12.2018 / 20:25