html - 使用 express 在 node.js 中获取选定的选项值
问题描述
我需要使用 express 获取所选对象以在 app.js 中对其进行控制台
例子.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>
应用程序.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')
});
即使我选择另一个对象,控制台的输出也是未定义的。
解决方案
您需要为post
表单提交的方法添加处理程序。
应用程序.js
app.post('/getJson', function (req, res) {
console.log(req.body.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>
推荐阅读
- android - Android Studio:BottomNavigationView和片段之间的事务
- mongodb - 有没有办法提高 MongoDB 的全文搜索性能?
- linux - 使用管道在所有 git 分支中对所有同名文件的属性进行 grep
- java - 如何等待 Apache Camel JDBC 作业完成
- sql - SQL Server 2019 Standard 无法从 2012 Enterprise 备份证书创建证书
- excel - 如何在 TextBoxes 中使用“动态搜索”过滤两列或多列
- php - 如何使用 Laravel 6 将数组值推送到数据库中?
- html - bash:将 html 实体转换为 UTF-8,但保留现有的 UTF-8
- excel - 根据使用 VBA 的 If 语句有粗底边框
- android - 奇怪的 AsyncTaskLoader 行为