收到从请求库传递的参数,但是从URL发送参数时不会收到它们

时间:2015-12-16 09:33:06

标签: javascript node.js

我有一个node.js服务器,我正在发出GET个请求。 使用Python的requests和JavaScripts的request传递的参数被正确接收。

// Python
requests.get(url, data=data)

// JS
request.get({url: url, form:form}, function(){})

但是,当我从以下浏览器发出请求时,未收到数据:url?a=1&b=2

我该如何解决?

这是我用来解析表单数据的函数:

function extractData(request, response, callback, options) {
  var jsonString = '';
  request.on('data', function(data) {
    jsonString += data;
  });
  request.on('end', function() {
    data = qs.parse(jsonString);
    callback(request, response, data, options);
  });
}
当我从URL传递参数时,

data为空。

1 个答案:

答案 0 :(得分:0)

我希望这会有所帮助,下面的代码对我有用

var options = {
      host: 'services.com',
      path: '/app/server.js/index/',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json; charset=utf-8',
        'Content-Length': data.length
      }
    };

    var req = https.request(options, function(res) {
      var msg = '';

      res.setEncoding('utf8');
      res.on('data', function(chunk) {
        msg += chunk;//'chunk' response from server
        if(chunk != "available")
        {
            var mail = {
                from: "abc@gmail.com",
                to: "ddd@gmail.com",
                subject: "Alert! from production server",
                html: "<p>Hi Team,</p> <p>Alert! production server is down.</p><p>Thanks,<br/>Maintenance Team</p>"
            };

            transporter.sendMail(mail, function(error, response){
                if(error){
                    console.log(error);
                    //res.send({ "result" : "failure" });
                }else{
                    console.log("Message sent: " + response);
                    //res.send({ "result" : "success" });
                }
            });
        }
      });
      res.on('end', function() {
        //console.log(JSON.parse(msg));
        console.log(msg);
      });