[英]how to read html request body from nodejs by name
嗨,我想從HTML頁面讀取單選按鈕檢查結果
我做了這個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>
<% } %>
<% } %>
<% } %>
已完成
QuestionList和AnswerList是顯示問題和答案的數組
我想得到結果到我的nodejs路由器,我的當前代碼如下
app.post('/question', function(req, res){
for(var i=0; i<questionList.length; i++){
console.log(req.body.questionList[i]);
}
});
但我收到錯誤TypeError: Cannot read property '0' of undefined at /home/kwon/NODE/TASK/myChat/routes/routes.js:298:37 at callbacks
如果我打印req.body的日志,那么我得到以下結果
{ 'Howoldareyou? ': '20', 'Whatisyourfavorite? ': 'sports' }
所以我認為它們保存在req.body中,我不知道如何讀取“ Howoldareyou”和“ Whatisyourfavorit”數據(每個數據都存儲在questionList中)
嘗試這個:
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.