我有一个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
为空。
答案 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);
});