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

145
Visualizações
Using AJAX to display a message on page after HTTP POST request

On form submission I would like to stay on the same page and display a message (either success or failure). The HTTP POST request is handled as follows:

app.post('/insertdb', function(request, response) {
   // insert values from form into database
   response.json({success: true});
});

In the client side .ejs file I then try to use ajax to display the alert (I tried following the example from how to use NodeJS pop up a alert window in browser) as follows:

    <script>
      $.ajax({
        url: "/dbinsert",
        type: "post",
        data: 'testing',
        cache: false,
        success: function(response) {
          if(data['success']) {
            alert("success");
          }
          
        },
        error: function () {
          alert("There has been an error");
        }
      });
    </script>

The issue is that when I press the submit button it takes me off the page and displays: ```{"success":true}`` instead of staying on the same page after the form submission and displaying a popup with the status of the request.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

see here: Prevent redirect after form is submitted

you need to add return false; to the end of your AJAX request :)

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