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

319
Visualizações
i tried to upate my ejs page with fetch but it doesn't work

i tried to build a simple app but i got a problem my app is just some peoples data saved in the db and the target is just to display them on the screen but we can search those people by name. i have a ejs page and javascript file connected to him

js code:

document.querySelector(".search__box_input").addEventListener("click", async function(event) {
    fetch("/", {
        method: "get",
        headers: new Headers({'search': searchButton.value})
    })
})

node code:

app.get("/", async(req, res) => {
    if(!req.headers["search"] || req.headers["search"] === ''.trim()) {
        return res.render("home", persons: await personSchema.find({}).limit(10)}) // personSchema is my mongoose schema
    } else {
        const foundDocuments = await personSchema.find({"name.first": req.headers["search"]})
        console.log(foundDocuments); // i got my document correctly
        if(!foundDocuments) return res.send("could'nt find the user")
        return res.render("home", {
            persons: foundDocuments // It doesn't render again
        })
    }
})

i want to render the ejs again.. someone can help me pls?

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

0

The point of Ajax is that you make a request with JavaScript and the response is given back to your JavaScript to process. Typically this would involve doing a DOM update.

Your JavaScript is completely ignoring the response.

const response = await fetch(...);
const text = await response.text();
console.log(text); 

If you want to render the whole page again, then don't use Ajax. Just use a regular form submission.

(And put the search parameter in the query string instead of a custom header).

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