我想返回从“ JSON.stringify(result))”获得的结果 我做了一个请求,这返回了一些数据。 我试图返回“结果结束后,但我有错误。 我该如何实现我的代码以将其返回?!
谢谢
var r = function requete(val) {
const xml = `----------`;
//MES REQUIRES
var https = require("https");
var fs = require("fs");
var parseString = require("xml2js").parseString;
var options = {
hostname: "----------",
path: "------------",
method: "POST",
headers: {
"Content-Type": "text/xml; charset=utf-8",
"Content-Length": xml.length
}
};
//My request
var req = https.request(options, res => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding("utf8");
var data=''
res.on("data", chunk => {
data+=chunk;
parseString(data, function(err, result) {
console.log(".............................");
console.log(JSON.stringify(result));
//=====>>>>>>i want to return this "result"
});
});
res.on("end", () => {
console.log("No more data in response.");
});
});
req.on("error", e => {
console.log(`problem with request: ${e.message}`);
});
// write data to request body
req.write(xml); // xml would have been set somewhere to a complete xml document in the form of a string
req.end();
};