节点服务器接收XmlHttpRequest

时间:2012-12-31 03:04:27

标签: json node.js xmlhttprequest webrtc

我正在使用以下代码发送会话描述(微小的JSON代码 - http://www.ietf.org/rfc/rfc2327.txt)。

function sendMessage(message) {
  var msgString = JSON.stringify(message);
  console.log('C->S: ' + msgString);
  path = '/message?r=67987409' + '&u=57188688';
  var xhr = new XMLHttpRequest();
  xhr.open('POST', path, true);
  xhr.send(msgString);
}

我不确定如何在我的Node.js服务器上检索JSON。

1 个答案:

答案 0 :(得分:6)

这是一个可以在node.js中处理POST请求的代码。

var http = require('http');

var server = http.createServer(function (request, response) {
    if (request.method == 'POST') {
        var body = '';
        request.on('data', function (data) {
            body += data;
        });
        request.on('end', function () {

            var POST = JSON.parse(body);
            // POST is the post data

        });
    }
});
server.listen(80);

希望这可以帮到你。