在节点js请求中使用GET方法?

时间:2014-11-24 08:04:00

标签: node.js

我想在节点js中使用GET方法传递数据,我的响应没问题但在php文件中$ _GET为空。怎么了?

var querystring = require('querystring');
var req = require('request');
var form = {
    number: 'test',
    msg: 'test',
};

var formData = querystring.stringify(form);
var contentLength = formData.length;

req({

    headers: {
      'Content-Length': contentLength,
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    uri: 'http://localhost:8080/sms/index.php',
    body: formData,
    method: 'GET'

  }, function (err, res, body) {
        console.log('res is',res);
        console.log('err',err);
  });

1 个答案:

答案 0 :(得分:3)

HTTP GET请求不能包含请求正文。 $_GET解析查询字符串中的参数。标头Content-LengthContent-Type对GET请求没有意义,因为它们适用于GET不能拥有的请求正文。您需要使用qs选项而不是body

req({
    uri: 'http://localhost:8080/sms/index.php',
    qs: form,
    method: 'GET'
  }, function (err, res, body) {
        //
  });