小编Vio*_*ham的帖子

表单 POST 请求正文为空 - Node.js / express / ejs / body-parser

我在使用 Node.js 中的注册表单发布帖子时遇到问题。每次发布表单时,请求的正文都有信息,但正文是空的。我对节点和表达很陌生,所以这可能是小事,但我一生都无法弄清楚。

这是我在 ejs 中的表单:

<form class="form-signin" method="post" action="/users/register">
  <div class="form-group">
    <label class="sr-only" for="email">Email address</label>
    <input type="email" class="form-control" id="email" placeholder="Enter Email" autocomplete="off">
  </div>
  <div class="form-group">
    <label class="sr-only" for="password">Password</label>
    <input type="password" class="form-control" id="password" placeholder="Enter Password">
  </div>
  <div class="form-group">
    <label class="sr-only" for="confirmPassword">Confirm Password</label>
    <input type="password" class="form-control" id="confirmPassword" placeholder="Re-enter Password">
  </div>
  <button class="btn btn-dark mt-3" type="submit">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)

这是我在用户路由器中的路由器代码:

router.post('/register', function(request, response, next) {
  console.log(request);
  console.log(request.body)
  response.render('register', { 'title': 'Register' });
});
Run Code Online (Sandbox Code Playgroud)

这是控制台上的相关输出:

console.log(request); output shortened this is just the tail.      
  _startAt: [ …
Run Code Online (Sandbox Code Playgroud)

forms ejs node.js express body-parser

5
推荐指数
1
解决办法
4183
查看次数

标签 统计

body-parser ×1

ejs ×1

express ×1

forms ×1

node.js ×1