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

237
Visualizações
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 Respostas
Responde à pergunta

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 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