Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda