HTTP request (POST)

Asked

Viewed 57 times

-2

Hello, I’m studying Angular and to train I’m doing a small Web application, but I’m having problems with the Post:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Can anyone help me? The error message that appears in the browser is this:

inserir a descrição da imagem aqui

  • Gustavo welcome to the stack overflow, please post the code as text according to the rules of the site

  • Try to remove stringify when sending

  • Gustavo, don’t forget to define a valid answer to your question.

1 answer

3


Gustavo,

Make sure you have a valid form.value and request without Stringfy as Eduardo mentioned. Ideally, use a Game object - just as it is done in the service - and associate the value of the form with it.

export class Game{
  public nome: string;      

  public constructor(init?: Partial<Game>) {
    Object.assign(this, init);
  }
}

onSubmit(form) {
         Game game = new Game(form.value);
         this.http.post<Game>('http://localhost:5000/api/games', game, this.options)
         .subscribe(dados => Console.log(dados));
}

If the object is validated you can receive it properly in the service as follows.

[HttpPost]
public void Post([FromBody] Game game)
{
    if(game != null)
    {
       GameDAO.Insert(game.nome);
    }
}

The appropriate thing when asking a question is to insert the code snippet because it helps a lot in the answers.

Browser other questions tagged

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