我需要使用express获取所选对象以在app.js中对其进行控制台
example.html
<form id="tableForm" action="getJson">
<select class="example" name="example">
<option name="" value="0" selected>Select table</option>
<option name="table1" value="1">Table 1</option>
<option name="table2" value="2">Table 2</option>
<option name="table3" value="3">Table 3</option>
</select>
</form>
App.js
var express = require('express'),
app = express();
app.use(express.bodyParser());
app.get('/', function(req, res){
res.sendfile('views/index.html');
});
app.get('/getJson', function (req, res) {
console.log(req.body.example);
});
app.listen(3000, function(){
console.log('Server running at port 3000: http://127.0.0.1:3000')
});
即使选择其他对象,控制台的输出也未定义。
答案 0 :(得分:0)
您需要在表单提交时为post
方法添加处理程序。
app.js
app.post('/getJson', function (req, res) {
console.log(req.body.example);
});
example.html
<form method="post" id="tableForm" action="getJson">
<select class="example" name="example">
<option name="" value="0" selected>Select table</option>
<option name="table1" value="1">Table 1</option>
<option name="table2" value="2">Table 2</option>
<option name="table3" value="3">Table 3</option>
</select>
</form>
答案 1 :(得分:0)
尝试
app.use(express.bodyParser());
代替
{{1}}
答案 2 :(得分:0)
这是您的代码中的错误
1)在HTML文件中,您的表单应具有post方法
if(["C", "D", "E", "F", "G", "A", "B"].includes(give)) { // ...
(2)该post方法应在App.js中处理。用户提供的输入值可以通过req.body.example方法收集