Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
fecth api not posting data to nodeserver

i have a node backend set up, when i send a get request, it works perfectly, but when i send a post request it doesn't send the data to the backend. Here is my code:

  fetch("http://localhost:3000/", { method: "POST",body:{"title":title.value,"content":content.value}})
    .then((res) => res.json())
    .then((data) => {
      console.log(data);
    });

i have even tried sending it through the formdata object it is still not working:

  const formdata = new FormData();
    formdata.append("title",title.value)
    formdata.append("content",content.value)


    fetch("http://localhost:3000/", { method: "POST",body:formdata})
      .then((res) => res.json())
      .then((data) => {
        console.log(data);
      });

this is the console.log: enter image description here

this is the backend:

//crud applications
app.post("/", async (req, res) => {
  const { title, content } = req.body;

  try {
    const newPost = await postModel.create({ title, content });
    res.json(newPost);
  } catch (error) {
    res.status(500).send(error);
  }
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

instead of using fetch, use Axios. import it in your HTML with this CDN <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

and then use this code it should work:

  const data = {
      title: title.value,
      content: content.value,
    };
    axios.post("http://localhost:3000/", data).then((response) => {
      const result = response.data;
      console.log(result);
     
    });
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda