无法读取POST的值

问题描述 投票:0回答:1

我无法从简单的表单中获取值。它有 2 个文件:

APP.js
FORM.ejs
。我正在尝试从表单中接收值。

APP.js

const http = require('http');
const express = require("express");
const S1_APP = express();
const ejs = require("ejs"); 
const bodyParser = require("body-parser"); 
S1_APP.use(bodyParser.urlencoded({ extended: false }));
S1_APP.set('view engine', 'ejs');//template engine

S1_APP.get("", function (req, res) {
    res.render('FORM.ejs');  
});

S1_APP.post("/",function(req, res) {

// here I need to read the value (myvalue 1 or 2) 
// that is send via the form in FORM.ejs
// I have tried req.body, that is empty
// I have tried req.value, that is undefined

});
S1_APP.listen(8080, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT 8080");
});

FORM.ejs

<html lang="en" >
<head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"></head>

<body>
    <form method="POST" action="/">
        <button type="submit" value="myvalue1"  >POST 1</button>
        <button type="submit" value="myvalue2"  >POST 2</button>
    </form>
</body>
</html>
javascript node.js forms express ejs
1个回答
0
投票

您需要添加

name
属性到表单控件/元素:

    <button name="control1" type="submit" value="myvalue1"  >POST 1</button>
    <button name="control2" type="submit" value="myvalue2"  >POST 2</button>

参见:

name

元素的名称。例如服务器用来识别 表单提交中的字段。

https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes

© www.soinside.com 2019 - 2024. All rights reserved.