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

230
Vistas
Front end JS fetch(POST) request being sent as a GET?

So this fetch request is called on the submit button.

For some reason, in the dev tools, it goes through as a GET. Tested the request in Insomnia, and it ends up returning the site to me (the handlebars site)... and none of my console logs ever show up (on the backend or the front)

Front End

const newPostSubmit= async function(event) {
    console.log('didnt make it');
    event.preventDefault()
    console.log('made it');

    const title = document.getElementById('post-title').value;
    const content = document.getElementById('content').value;

    const response = await fetch(`/api/posts/`, {
        method: 'POST',
        body: JSON.stringify({
            title,
            content
        }),
        headers: {
            'Content-Type': 'application/json'
        }
    });

    if (response.ok) {
        console.log('Post Success!')
        // document.location.replace('/dashboard');
    } else {
        alert(response.statusText);
    }
}

document.getElementById('submit-post').addEventListener('submit', newPostSubmit);

Back End Route To Be Used

router.post('/', checkAuth, (req, res) => {
    const body = req.body
    console.log(body);

    Post.create({ ...body, userId: req.session.userId })
    .then(dbPostData => {
        console.log(dbPostData);
        res.json(dbPostData)
    })
    .catch(err => {
        console.log(err);
        res.status(500).json(err);
    });
});

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The problem was with my HTML. The id of 'submit-post' was placed on the button, but needed to be on the form itself (the button was within the element) which is why the function would never run

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