简体   繁体   English

jQuery + node.js表达POST请求

[英]jQuery + node.js express POST request

I'm trying to send a post request to a node server. 我正在尝试向节点服务器发送post请求。 This is my client-side code: 这是我的客户端代码:

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. 但是当我记录响应时, body{ }对象始终为空。 Why is that? 这是为什么? Is a <form> tag necessary for POST requests? POST请求是否需要<form>标记?

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作为dataType并对数据进行字符串化,但req.body仍为空。

$.ajax({
    method: "POST",
    url: "/chat/messages",
    data : JSON.stringify({'id' : userid, 'message' : message}),
    dataType: 'json',
})

When you post data to a server, the data is usually urlencoded and added to the body of the request. 将数据发布到服务器时,数据通常是urlencoded并添加到请求的正文中。 In your example, it would look like this: 在您的示例中,它看起来像这样:

id=<userid>&message=<message>

Therefore, the bodyparser you need to be able to parse that is bodyparser.urlencoded() 因此,你需要能够解析的bodyparser.urlencoded()bodyparser.urlencoded()

app.use(bodyParser.urlencoded())

Note that it isn't always urlencoded, it all depends on what you are using to send the post. 请注意,它并不总是urlencoded,它取决于您用来发送帖子的内容。 AngularJS for example defaults to sending it as json instead. 例如,AngularJS默认将其作为json发送。 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. 好消息是你可以简单地添加两个bodyparser并且你的路由不需要知道使用了哪种方法,因为在这两种情况下数据最终都会出现在具有键/值对的req.body上。

You should read the express documentation. 您应该阅读快速文档。 http://expressjs.com/api.html#req http://expressjs.com/api.html#req

// 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)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM