我想在节点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);
});
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) {
//
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句