【问题标题】:Pug and nodejs/express - button sends requestPug 和 nodejs/express - 按钮发送请求
【发布时间】:2017-01-28 12:47:32
【问题描述】:

我有一个使用 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})
});

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

【问题讨论】:

标签: node.js express


【解决方案1】:

你应该能够通过

获取你的价值
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})
});

【讨论】:

    猜你喜欢
    • 2019-07-21
    • 1970-01-01
    • 2011-10-19
    • 2013-01-03
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多