【问题标题】:Express cannot process textarea value [duplicate]Express 无法处理 textarea 值 [重复]
【发布时间】:2019-04-22 08:41:36
【问题描述】:

我想在 node.js 中使用 body-parser 的 post 方法获取价值。但我总是不确定。

下面是我的 node.js 代码。

var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });

app.get('/form', function (req, res) {
  html += "<form action='/thank'  method='post' name='form1'>";
  html += "<div id='divParent'>";
  html += "<div id='div1' name='formset' class='formset'>";
  html += "<p>Name: <input type= 'text' name='name'></p>";
  html += "<p>Sex: Male <input type='radio' class='gender' name='gender' value='male'>"
  html += " Female <input type='radio' class='gender' name='gender' value='female'></p>"
  html += "<p>Email: <input type='text' name='email'></p>";
  html += "<p>Address:<input type='text' name='address'></p>";
  html += "<p>Mobile number:<input type='text' name='mobilno'></p>";
  html += "<p>Note:</p>"
  html += '<textarea rows="4" cols="50" name="note" id="note" form="form1"></textarea>';
  html += "</div>";
  html += "</div>";
  html += "<input id='input1' type='submit' value='submit'>";
  html += "<INPUT type='reset'  value='reset'>";
  html += "</form>";

  ...
}

app.post('/thank', urlencodedParser, function (req, res){
  var reply='';
  reply += "<br>Array length is : " + req.body.name.length;
  reply += "<br>Your name is : " + req.body.name;
  reply += "<br>Sex is : " + req.body.gender;
  reply += "<br>Your E-mail id is : " + req.body.email; 
  reply += "<br>Your address is : " + req.body.address;
  reply += "<br>Your mobile number is : " + req.body.mobilno;
  reply += "<br>Your note is : "+ req.body.note;
  console.log(req.body)
  res.send(reply);
});

我可以获取输入值,但不能获取 textarea 值。 这是我在 console.log(req.body) 时得到的结果

{ name: '罗伊', 性别:'男性', 电子邮件:'roy@topscore.com', 地址:'巴厘岛', mobilno: '0821' }

为什么 body-parser 无法获取 req.body.note?有什么问题?

【问题讨论】:

    标签: node.js express body-parser


    【解决方案1】:

    我认为 textarea 并不是表单的一部分,因为您没有 ID 为“form1”的表单。从 textarea 中删除 form 属性或将属性 id 添加到表单。

    【讨论】:

    • 是的,我在表单中添加了 id。它有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    相关资源
    最近更新 更多