[英]how to read html request body from nodejs by name
Hi I want to read radio button check result from html page 嗨,我想从HTML页面读取单选按钮检查结果
I made this html page 我做了这个HTML页面
<form action="/question" method="post">
<% for (var i = 0; i < questionList.length; i++) { %>
<h1> <%= questionList[i] %> </h1> <br>
<% for (var j = 0; j < answerList[i].length; j++) { %>
<% if(j === 0) { %>
<input type="radio" name="<%= questionList[i] %>" id="<%= questionList[i] %>" value="<%=answerList[i][j] %>" checked="checked"> <%=answerList[i][j] %> <br>
<% } %>
<% if(j > 0) { %>
<input type="radio" name="<%= questionList[i] %>" id="<%= questionList[i] %>" value="<%=answerList[i][j] %>"> <%=answerList[i][j] %> <br>
<% } %>
<% } %>
<% } %>
Finished 已完成
questionList and answerList are array to show question and answer QuestionList和AnswerList是显示问题和答案的数组
I want to get a result to my nodejs router my current code is below 我想得到结果到我的nodejs路由器,我的当前代码如下
app.post('/question', function(req, res){
for(var i=0; i<questionList.length; i++){
console.log(req.body.questionList[i]);
}
});
but I got an error TypeError: Cannot read property '0' of undefined at /home/kwon/NODE/TASK/myChat/routes/routes.js:298:37 at callbacks
但我收到错误
TypeError: Cannot read property '0' of undefined at /home/kwon/NODE/TASK/myChat/routes/routes.js:298:37 at callbacks
If I print log of req.body then I got result below 如果我打印req.body的日志,那么我得到以下结果
{ 'Howoldareyou? ': '20', 'Whatisyourfavorite? ': 'sports' }
so I think they are saved in well in req.body I don't know how to read 'Howoldareyou' and 'Whatisyourfavorit' data (each are stored in questionList) 所以我认为它们保存在req.body中,我不知道如何读取“ Howoldareyou”和“ Whatisyourfavorit”数据(每个数据都存储在questionList中)
Try this: 尝试这个:
app.post('/question', function(req, res){
for(var i=0; i<questionList.length; i++){
console.log(req.body[questionList[i]] );
}
});
伙计,您需要bodyparser模块以这种方式接收数据,以检查bodyparser代码是否过旧,因为bodyparser早已包含在express中,但现在不再可用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.