在html页面的node.js中使用Express获取下拉值
我正在使用快速开发一个快速的node.js应用程序,我是NODE的新手。 对于页面我只是使用纯HTML。
基本上我有一个表格如下:
<form id="tableForm" action="getJson"> <select class="selectpicker" data-style="btn-info" name="selectpicker"> <optgroup label="Select Table"> <option name="" value="0">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> </optgroup> </select> </form>
基本上,我需要得到的值一旦完成我需要它通过app.get()
调用,但我的问题是我如何获得的价值,并调用API?
var express = require('express'), app = express(); app.use(express.bodyParser()); // as only one page can use res.sendfile to render the page which will // contain the dropdowns ... app.get('/', function(req, res){ res.sendfile('views/index.html'); }); app.get('/getJson', function (req, res) { console.log(req.body.); }); app.listen(process.env.PORT);
所以我需要调用getJson()
与传入的值。
干杯!
你需要以某种方式提交表单。 最简单的方法是使用提交button。 你也需要把表单的方法,这就像你想要使用GET一样。
HTML
<form id="tableForm" action="/getJson" method="get"> <select class="selectpicker" data-style="btn-info" name="selectpicker"> <optgroup label="Select Table"> <option name="" value="0">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> </optgroup> </select> <input type="submit" /> </form>
在服务器端,您需要parsing出获取请求。 你已经拥有了它,你只需要知道你在找什么。 由于你的select有名称“selectpicker”,这就是你将在这种情况下使用。
JavaScript的
var express = require('express'), app = express(); app.use(express.bodyParser()); // as only one page can use res.sendfile to render the page which will contain the drop downs app.get('/', function (req, res) { res.sendfile('views/index.html'); }); app.get('/getJson', function (req, res) { // If it's not showing up, just use req.body to see what is actually being passed. console.log(req.body.selectpicker); }); app.listen(process.env.PORT);
我没有完全testing这个代码,但它应该工作。
- mongooseselect子文件
- 如何使用mod_proxy在apache后面运行node.js时获取req.hostname
- 如何在使用Socket.IO时访问会话标识符?
- 在expressionjs res.render之前,使http请求获取JSON
- cloudinary多个.jpg上传NODE.JS
- nodejs – jade ReferenceError:进程没有定义
- 使用快递,如何加载控制器代码更改,而无需重新启动服务器?
- 如何在Node.JS中分别接收req.body.model的所有对象而不是它的所有variables?
- 用Jade编译Coffeescript:咖啡filter:会妨碍性能吗? 有没有办法简化? 裸?