我想在节点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);
});
答案 0 :(得分:3)
HTTP GET请求不能包含请求正文。 $_GET
解析查询字符串中的参数。标头Content-Length
和Content-Type
对GET请求没有意义,因为它们适用于GET
不能拥有的请求正文。您需要使用qs
选项而不是body
。
req({
uri: 'http://localhost:8080/sms/index.php',
qs: form,
method: 'GET'
}, function (err, res, body) {
//
});