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

353
Visualizações
how to get an answer? (no page reload)

the server sends a response. I don't understand how to process it. If you get "json" opens a page with array. If you get "res.render" refreshes the page. How can I get json and process it.

// express.js

if (!errors.isEmpty()) {          
    if (errors.array().find(el => el.param === 'login')) {
        //1 version
        res.status(409).json({
            message: 'Error login'
        })
        //2 version
        res.render('reg', {
            isReg: true
        })
    } 
}
 
 <!-- Handlebars -->
 <form class="login-form" action="/reg" method="POST" >
    <div class="form-group">
        <label for="exampleInputEmail1" class="text-uppercase">Login</label>
        <input type="text" class="form-control" id="validationLogin" name="login" placeholder="" required>
    </div>
        <div class="block-login d-flex justify-content-center">
        <button type="submit" class="btn btn-login float-right">Submit</button>
    </div>
</form>

<!-- how to get an answer?  -->

<script>
//example
var answer = res.json
</script>

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

0

If you use res.json then the end point will return JSON. This is designed to be consumed with some custom code and not by being loaded directly into the browser. You could make an Ajax request and then insert data from the result into the DOM. This is how you get the result without loading a new page.

If you use res.render then then end point will return whatever you render, which is almost always HTML. This is a new page. You are expected to pass the data as the second argument to render and then process it with the view engine you have selected (handlebars in this case).

e.g.

<p>{{isReg}}</p>
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