我在HTML中有以下表格:
<form method="post" id="registration-form" action="/register">
<div class="form-group">
<label for="UsernameRegistration">Username:</label>
<input type="text" class="form-control" id="UsernameRegistration">
</div>
<div class="form-group">
<label for="PasswordRegistration">Password:</label>
<input type="password" class="form-control" id="PasswordRegistration">
</div>
<div class="form-group">
<label for="ConfirmPasswordRegistration">Confirm Password:</label>
<input type="password" class="form-control" id="ConfirmPasswordRegistration">
</div>
<input type="submit" class="form-control" />
</form>
/register
个端点如下所示:
router.post('/register', function(req, res, next) {
console.log(req);
});
在req.query和req.body中,没有数据。我做错了什么?
答案 0 :(得分:5)
<input type="password" class="form-control" id="PasswordRegistration">
此处未指定属性名称。 它应该像
<input type="password" name="password" class="form-control" id="PasswordRegistration">
答案 1 :(得分:2)
您没有为输入元素提供name属性。
我为元素提供了name属性,例如:
<form method="post" id="registration-form" action="/register">
<div class="form-group">
<label for="UsernameRegistration">Username:</label>
<input name="username" type="text" class="form-control" id="UsernameRegistration">
</div>
<div class="form-group">
<label for="PasswordRegistration">Password:</label>
<input name="password" type="password" class="form-control" id="PasswordRegistration">
</div>
<div class="form-group">
<label for="ConfirmPasswordRegistration">Confirm Password:</label>
<input name="confpass" type="password" class="form-control" id="ConfirmPasswordRegistration">
</div>
<input type="submit" class="form-control" />
</form>
router.post("/registration", (req, res) => {
var username = req.params.username;
var pass = req.params.password;
var confpass = req.params.confpass;
})
您将在req.params对象中获取数据。
答案 2 :(得分:0)
我认为你错过了这两件事: -
1.您是否在我们的应用中添加了正文解析器? (包含请求正文中提交的键值对数据。默认情况下,它是未定义的,并在使用正文解析器等身体解析中间件时填充)
var app = require('express')();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded