我正在使用快速开发一个快速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>
Run Code Online (Sandbox Code Playgroud)
基本上,我需要在完成后选择值,我需要通过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);
Run Code Online (Sandbox Code Playgroud)
所以我需要调用getJson()传入的值.
干杯!
And*_*ely 13
你需要以某种方式提交表格.最简单的方法是使用提交按钮.你还需要为表单设置方法,顺便说一句,这听起来好像你想要使用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>
Run Code Online (Sandbox Code Playgroud)
在服务器端,您需要解析get请求.你已经设置好接收它,你只需要知道你在寻找什么.由于您的选择具有名称"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);
Run Code Online (Sandbox Code Playgroud)
我还没有完全测试这段代码,但它应该可行.
| 归档时间: |
|
| 查看次数: |
32267 次 |
| 最近记录: |