Doubts with post method at the angular

Asked

Viewed 50 times

0

Hello, I’m trying to make a POST method that returns me an object 'User'.

In my Backend I have the following code:

 //Add um Usuarios
 public Usuarios Post([FromBody]Usuarios usuario)
 {
     return _usuariosServices.Adicionar(usuario);
 }

That is, returns me an object 'User'.

At the angle I make the call of this method and the request arrives in the Backend, but I cannot play the return in an angle variable...

My Angular method ta as follows...

CriandoUsuario(usuario: UsuariosModel){
  let headers = new Headers();
  headers.append('Content-Type', 'application/json')

  this.http.post(`${API}` + 'Usuarios'
  , JSON.stringify(usuario)
  , { headers: headers })
  . subscribe(() => {});
 }

From what I understand, it is necessary to do something within the Subscribe, but I do not know what that something is... Thank you in advance...

1 answer

1


You are complicating unnecessarily. Then just subscribe to the function return.

httpOptions = {
  headers: new HttpHeaders({
    'Content-Type':  'application/json'      
})

constructor(private httpClient: HttpClient) {         
}


criarUsuario(usuario: UsuariosModel){
  return this.httpClient.post(`${API}Usuarios`, user, this.httpOptions)
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.