将表单值传递给节点中的 uri 并表达
passing form values to uri in node and express
这是我的 input.jade 文件
div
form(action='/wiki/<topic-name>',method='post')
div(data-role='fieldcontain')
fieldset(data-role='controlgroup')
label(for='topicname') Topicname
input(id='topicname',type='text',value='',placeholder='topicname',name='topicname')
div(data-role='fieldcontain')
input(type='submit',value='Sign Up',data-transition='fade', data-theme='c')
提交后,我希望它重定向到 /wiki/topic-name,其中 topic-name 由用户在表单中输入。
这是我的路线文件。
router.get('/input', function(req, res, next) {
res.render('content/input', { title: 'Express' });
});
router.get('/wiki/:topicname', function(req, res, next) {
var topicname = req.params.topicname;
//other code
});
应该怎么做?有没有更好的方法?
编辑:如果我不想通过 post 传递值,我应该如何进行? (制作表格方法后="get")。请注意,我希望它重定向到特定的 URI。
你应该只在 HTTP/Get 操作中使用 URI 参数,看起来你想发送一个 FORM post 而你的 router.get('/wiki/:topicname'
永远不会被调用,因为它正在等待一个 GET 而不是一个POST.
你应该这样做(代码注释):
div
form(action='/wiki',method='post') /*removed the uri param*/
div(data-role='fieldcontain')
fieldset(data-role='controlgroup')
label(for='topicname') Topicname
input(id='topicname',type='text',value='',placeholder='topicname',name='topicname')
div(data-role='fieldcontain')
input(type='submit',value='Sign Up',data-transition='fade', data-theme='c')
router.get('/input', function(req, res, next) {
res.render('content/input', { title: 'Express' });
});
router.post('/wiki', function(req, res, next) { // now route is getting POST
var topicname = req.body.topicname; // get params from body
//other code
});
正如另一个回复所指出的,您需要从表单操作中删除 :topicname。
假设您使用的是 Express 4,从输入字段中获取主题名称并重定向到 /wiki/topicname:
//your jade form submits here
router.post('wiki/', function(req, res, next) {
res.redirect('wiki/' + req.body.topicname);
});
//the post redirects to this page, where you do your work
router.get('wiki/:topicname', function(req, res, next) {
var topicname = req.params.topicname;
});
这是我的 input.jade 文件
div
form(action='/wiki/<topic-name>',method='post')
div(data-role='fieldcontain')
fieldset(data-role='controlgroup')
label(for='topicname') Topicname
input(id='topicname',type='text',value='',placeholder='topicname',name='topicname')
div(data-role='fieldcontain')
input(type='submit',value='Sign Up',data-transition='fade', data-theme='c')
提交后,我希望它重定向到 /wiki/topic-name,其中 topic-name 由用户在表单中输入。
这是我的路线文件。
router.get('/input', function(req, res, next) {
res.render('content/input', { title: 'Express' });
});
router.get('/wiki/:topicname', function(req, res, next) {
var topicname = req.params.topicname;
//other code
});
应该怎么做?有没有更好的方法?
编辑:如果我不想通过 post 传递值,我应该如何进行? (制作表格方法后="get")。请注意,我希望它重定向到特定的 URI。
你应该只在 HTTP/Get 操作中使用 URI 参数,看起来你想发送一个 FORM post 而你的 router.get('/wiki/:topicname'
永远不会被调用,因为它正在等待一个 GET 而不是一个POST.
你应该这样做(代码注释):
div
form(action='/wiki',method='post') /*removed the uri param*/
div(data-role='fieldcontain')
fieldset(data-role='controlgroup')
label(for='topicname') Topicname
input(id='topicname',type='text',value='',placeholder='topicname',name='topicname')
div(data-role='fieldcontain')
input(type='submit',value='Sign Up',data-transition='fade', data-theme='c')
router.get('/input', function(req, res, next) {
res.render('content/input', { title: 'Express' });
});
router.post('/wiki', function(req, res, next) { // now route is getting POST
var topicname = req.body.topicname; // get params from body
//other code
});
正如另一个回复所指出的,您需要从表单操作中删除 :topicname。
假设您使用的是 Express 4,从输入字段中获取主题名称并重定向到 /wiki/topicname:
//your jade form submits here
router.post('wiki/', function(req, res, next) {
res.redirect('wiki/' + req.body.topicname);
});
//the post redirects to this page, where you do your work
router.get('wiki/:topicname', function(req, res, next) {
var topicname = req.params.topicname;
});