Send a POST and how do I recover the data in ANGULAR?

Asked

Viewed 150 times

-1

onSubmit() {
console.log(this.formulario.value)

this.http.post(this.configUrl, (this.formulario.value)).pipe(
 map(res => res))
.subscribe(dados => console.log(dados))   
}

This is my post code, I’m sending a message to the server and it returns me a JSON , as I would to retrieve this to you can insert on my screen ?

1 answer

0

Good morning Fernando, the http POST method is to record the data only, you only receive the code referring to its status (success, error or others). To recover the data you recorded and display on the screen, you need to perform another request, but this time with the GET method.

Would that way:

At your service:

recuperaDados() {
  return this.http.get(this.getURL);  
}

In your component, you can do it with ASYNC PIPE, or just by registering to return:

this.servico.recuperaDados().subscribe(val => console.log(val));

This way only save in a local variable that has access to the template (HTML).

I hope I helped, try to take a look at the documentation in the part of Dependency Injection, follow the link https://angular.io/guide/dependency-injection

Browser other questions tagged

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