I'm trying to send a post request to a node server. This is my client-side code:
我正在尝试向节点服务器发送一个post请求。这是我的客户端代码:
function send(userid,message){
$.ajax({
method: "POST",
url: "/chat/messages?id="+userid+'&message='+message
})
clear();
}
This is my server side code:
这是我的服务器端代码:
app.post('/chat/messages',function (req,res){
var query = url.parse(req.url,true).query
insertMessage(query.id,query.message)
})
This works, however I'm not sure getting data in the query string using post is the right way to go.
这是可行的,但是我不确定使用post在查询字符串中获取数据是否正确。
I tried adding a data field in $ajax
parameter:
我尝试在$ajax参数中添加一个数据字段:
function send(userid,message){
$.ajax({
method: "POST",
url: "/chat/messages"
data : {'id' : userid, 'message' : message}
})
clear();
}
And using bodyParser()
in the server end to parse the body contents:
并在服务器端使用bodyParser()解析主体内容:
app.use(bodyParser.json())
app.post('/chat/messages',function (req,res){
console.log(req.body)
})
but when I log the response, the body{ }
object is always empty. Why is that? Is a <form>
tag necessary for POST requests?
但是当我记录响应时,body{}对象总是空的。这是为什么呢?对于POST请求是否需要一个
I tried editing my ajax request to use json
as the dataType and stringifying the data, but the req.body
is still empty.
我尝试编辑ajax请求,使用json作为数据类型,并对数据进行字符串化,但是使用req。身体仍然是空的。
$.ajax({
method: "POST",
url: "/chat/messages",
data : JSON.stringify({'id' : userid, 'message' : message}),
dataType: 'json',
})
2 个解决方案
#1
2
When you post data to a server, the data is usually urlencoded and added to the body of the request. In your example, it would look like this:
当您向服务器发布数据时,数据通常是urlencoding的,并添加到请求的主体中。在你的例子中,它看起来是这样的:
id=<userid>&message=<message>
Therefore, the bodyparser you need to be able to parse that is bodyparser.urlencoded()
因此,您需要能够解析的bodyparser. urlencodes()的bodyparser
app.use(bodyParser.urlencoded())
Note that it isn't always urlencoded, it all depends on what you are using to send the post. AngularJS for example defaults to sending it as json instead. The good news is you could simply add both bodyparsers and your route won't need to know which method was used since in both cases the data would end up on req.body with key/value pairs.
注意,它并不总是urlencodes,这一切都取决于你用什么来发送邮件。例如,AngularJS默认将其发送为json。好消息是,您可以简单地添加两个bodyparser,您的路由不需要知道使用了哪个方法,因为在这两种情况下,数据都将在req上结束。身体与键/值对。
#2
1
You should read the express documentation. http://expressjs.com/api.html#req
您应该阅读express文档。http://expressjs.com/api.html请求
// For regular html form data
app.use(bodyParser.urlencoded())
app.post('/chat/messages',function (req,res){
console.log(req.body);
console.log(req.query.id);
console.log(req.query.messages);
})
You can also do req.params
您还可以执行req.params
app.post('/chat/messages/:id',function (req,res){
console.log(req.body);
console.log(req.query);
console.log(req.params.id)
})
#1
2
When you post data to a server, the data is usually urlencoded and added to the body of the request. In your example, it would look like this:
当您向服务器发布数据时,数据通常是urlencoding的,并添加到请求的主体中。在你的例子中,它看起来是这样的:
id=<userid>&message=<message>
Therefore, the bodyparser you need to be able to parse that is bodyparser.urlencoded()
因此,您需要能够解析的bodyparser. urlencodes()的bodyparser
app.use(bodyParser.urlencoded())
Note that it isn't always urlencoded, it all depends on what you are using to send the post. AngularJS for example defaults to sending it as json instead. The good news is you could simply add both bodyparsers and your route won't need to know which method was used since in both cases the data would end up on req.body with key/value pairs.
注意,它并不总是urlencodes,这一切都取决于你用什么来发送邮件。例如,AngularJS默认将其发送为json。好消息是,您可以简单地添加两个bodyparser,您的路由不需要知道使用了哪个方法,因为在这两种情况下,数据都将在req上结束。身体与键/值对。
#2
1
You should read the express documentation. http://expressjs.com/api.html#req
您应该阅读express文档。http://expressjs.com/api.html请求
// For regular html form data
app.use(bodyParser.urlencoded())
app.post('/chat/messages',function (req,res){
console.log(req.body);
console.log(req.query.id);
console.log(req.query.messages);
})
You can also do req.params
您还可以执行req.params
app.post('/chat/messages/:id',function (req,res){
console.log(req.body);
console.log(req.query);
console.log(req.params.id)
})