【问题标题】:HTML form not sending data in ExpressHTML 表单未在 Express 中发送数据
【发布时间】:2017-03-25 06:06:59
【问题描述】:

我的 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 中,没有数据。我做错了什么?

【问题讨论】:

    标签: html node.js express


    【解决方案1】:
    <input type="password" class="form-control" id="PasswordRegistration">
    

    这里没有指定属性name。 应该是这样的

    <input type="password" name="password" class="form-control" id="PasswordRegistration">
    

    【讨论】:

      【解决方案2】:

      您没有为输入元素提供名称属性。

      我为元素提供名称属性,例如:

      <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 对象中获取数据。

      【讨论】:

        【解决方案3】:

        我认为你错过了这两件事:-

        1.您是否在我们的应用中添加了body parser?? (包含请求体中提交的数据键值对,默认未定义,使用body-parser等body-parser中间件时填充)

        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
        
        1. 表单元素中缺少 name 属性

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-27
          • 1970-01-01
          • 2017-11-10
          • 1970-01-01
          • 2020-11-08
          相关资源
          最近更新 更多