[英]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
我有一个 ejs 页面和 javascript 文件连接到他
js code: js代码:
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?我想再次渲染 ejs .. 有人可以帮我吗?
The point of Ajax is that you make a request with JavaScript and the response is given back to your JavaScript to process. Ajax 的要点是您向 JavaScript 发出请求,并将响应返回给您的 JavaScript 进行处理。 Typically this would involve doing a DOM update .
通常这将涉及进行DOM 更新。
Your JavaScript is completely ignoring the response.您的 JavaScript 完全忽略了响应。
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.如果要再次渲染整个页面,则不要使用 Ajax。 Just use a regular form submission.
只需使用常规表单提交即可。
(And put the search
parameter in the query string instead of a custom header). (并将
search
参数放在查询字符串中,而不是自定义标题中)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.