简体   繁体   English

我试图用 fetch 更新我的 ejs 页面,但它不起作用

[英]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.

相关问题 我已经尝试过,但是我的程序似乎不起作用 - I have tried but my program doesn't seem to work 为什么我的脚本文件在 index.ejs 中不起作用? - Why doesn't my script file work in index.ejs? 我的 headerStyle 不起作用。 我试图分配背景颜色,但在我运行它时它没有出现 - My headerStyle doesn't work. I have tried to assign a background color though it doesn't appear when I run it 为什么我的简单 if 检查,如果用户在那里,在我的 ejs 视图中抛出一个错误。 如果 var 不存在,我指定其他内容,但仍然不起作用 - Why is my simple if check, if user is there, throwing an error in my ejs view. If var not there, I specify something else, but still doesn't work 如何从父页面获取属性值。我尝试使用opener和window.parent.But不起作用 - How to get the attribute value from parent page.I tried using opener and window.parent.But doesn't work 如果带脚本标签的EJS中有条件的条件不起作用 - IF conditional in EJS with Script tag doesn't work 我尝试使用 .map function 但它对我的代码不起作用 - I tried to use the .map function but it didn't work on my code jQuery 切换在我的页面中不起作用 - jQuery toggle doesn't work in my page 在 OpenLayers 6.9.0 中,我使用 fetch WMTSCapabilities 但它不起作用 - In OpenLayers 6.9.0 I use fetch WMTSCapabilities but I it doesn't work 加载html页面时,为什么我的外部javascript文件不起作用? - Why doesn't my external javascript file work when I load my html page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM