gpt4 book ai didi

node.js - Pug 和 Nodejs/express - 按钮发送请求

转载 作者:太空宇宙 更新时间:2023-11-04 02:11:26 25 4
gpt4 key购买 nike

我有一个使用 Express 的 NodeJS 服务器。它使用哈巴狗模板。它显示一堆图像。我希望它在每个图像上显示一个按钮。当按下该按钮时,服务器会删除图像并刷新客户端。

这是我到目前为止所得到的 .pug:

// register form
block content
form(name="getomdb",method='post')
div.input
input(type="submit",name="delete", value="Delete " + link[0])

div.container
h3#title
p#plot

link[0] 引用了我要删除的图像。据我所知,这是在向我的服务器发送一个 POST 请求,其值为“Delete” + link[0]。

我的服务器尝试处理它:

    app.post('/', function(req, res){
console.dir(req.body);
console.log('post got');
res.render('index.pug', { links: links})
});

显然,请求没有正文(它打印未定义)。我如何访问它试图删除的特定链接?

最佳答案

您应该能够通过以下方式访问您的值(value)

req.body.delete

只需添加您要传入的值的名称。请记住,如果您开始传入 json 或其他格式,则需要查看“BodyParser”模块。

    app.post('/', function(req, res){
console.dir(req.body.delete); //add the .delete
console.log('post got');
res.render('index.pug', { links: links})
});

关于node.js - Pug 和 Nodejs/express - 按钮发送请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41905913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com