Doubts about fetch

Asked

Viewed 71 times

0

Good afternoon, you guys! So, I’m new to the stack, it’s a pleasure.

I created an api on. net core for studies, and I’m creating a POST method but it doesn’t work at all, not even add to my JSON, it just gives a refresh pattern and with that, back from scratch with the json already added in my api!

My method doesn’t work at all.

Anyway, the request doesn’t work. Can someone help me?

Get method works normally, so much so that I took the data from my api and returned.

//Route is like this because of the CORS, but works normally.

For me to add a person via Postman via JSON: https://localhost:5000/Person via post.

fetch('/Pessoa/BuscarTodos').then(
res => {
    res.json().then(
        data => {
            console.log(data);
            if(data.length > 0)
            {
                var temp = "";
                data.forEach((u) => {
                    temp += "<tr>";
                    temp += "<td>"+u.nome+"<td>";
                    temp += "<td>"+u.cpf+"<td>";
                    temp += "<td>"+u.dataNascimento+"<td>";
                    temp += "<td>"+u.uf+"<td>";
                })
                document.getElementById("data").innerHTML = temp;
            }
        })            
})

POST METHOD:


 document.getElementById('cadastrar').addEventListener('submit',

 function(event) {
     event.preventDefault();


let nome = document.getElementById('Nome').value;
let cpf = document.getElementById('CPF').value;
let dataNascimento = document.getElementById('DataNascimento').value;
let uf = document.getElementById('UF').value;

fetch('Pessoa', {
    Method: 'POST',
    headers: new Headers(),
    body: JSON.stringify({"nome": nome, "cpf": cpf, "dataNascimento": dataNascimento, "uf" : uf})
}).then((res) => res.json())
.then((data) => console.log(data))
.catch((erro) => console.log('erro', erro));

 })

Now the HTML:

<form id="postdata">
<div class="form-group text-center">
    <label for="nome">Nome</label>
    <input type="text" id="Nome" class="form-control" placeholder="Digite seu nome">
</div>
<div class="form-group text-center">
    <label for="cpf">CPF</label>
    <input type="text" id="CPF" class="form-control" placeholder="Digite seu CPF">
</div>
<div class="form-group text-center">
    <label for="dataNascimento">Data de nascimento</label>
    <input type= "date" id="DataNascimento" class="form-control" placeholder="Digite sua data de nascimento">
</div>
<div class="form-group text-center">
    <label for="uf">UF</label>
    <input type="number" id="UF" class="form-control" placeholder="Digite a sua unidade federativa">
</div>

<tr></tr>
<div class="botaoCadastrar text-center">
    <button id="cadastrar">
        <input type="submit"  class="btn btn-danger">
    </button>
</div>

  • 4

    The estate method should be written with m minuscule.

  • Did not resolve :/

  • You will have to give more details of this error. Have you opened the developer tools in your browser to see if there were any exceptions? Have you checked whether the request was successful in your network? Without these details there is no way to help you, since there is no way to reproduce your problem without access to this route.

  • I got it! It was because my form didn’t have method=post. Now it returns me a 404 when making the request.

No answers

Browser other questions tagged

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