问题描述
我有一个简单的表单,该表单通过Express将POST数据发送到我的Node.JS服务器.格式如下:
I've got a simply form, that sends POST data to my Node.JS server, with Express. This is the form:
<form method="post" action="/sendmessage">
<div class="ui-widget">
<input type="text" id="search" data-provide="typeahead" placeholder="Search..." />
</div>
<textarea id="message"></textarea>
</form>
ui-widget和输入与来自Twitter的自动完成库typehead
关联.这就是我在服务器中处理POST请求的方式:
The ui-widget and the input is releated with typehead
, an autocomplete library from Twitter.And this is how I handle the POST request in the server:
app.post('/sendmessage', function (req, res){
console.log(req.body);
usermodel.findOne({ user: req.session.user }, function (err, auser){
if (err) throw err;
usermodel.findOne({ user: req.body.search }, function (err, user){
if (err) throw err;
var message = new messagemodel({
fromuser: auser._id,
touser: user._id,
message: req.body.message,
status: false
});
message.save(function (err){
if (err) throw err;
res.redirect('/messages')
})
});
});
});
控制台向我显示'{}',然后显示req.body.search
错误,因为未定义search
.我不知道这里发生了什么,这不是与typehead
输入有关的问题.这个问题有解决方案吗??
The console shows me '{}', and then an error with req.body.search
, because search
is not defined. I don't know what is happening here, and it's not a problem related with the typehead
input. Any solution for this problem...?
谢谢前进!
推荐答案
req.body
由名称和值组成.
在搜索框中添加name="search"
,然后重试.
add name="search"
on your search box and try again.
还要感谢Nick Mitchinson,您还必须使用express/connect.bodyParser()中间件!
You also must use the express/connect.bodyParser() middleware, thanks Nick Mitchinson!
这篇关于使用Express node.js接收带有邮寄表单的req.body为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!