【发布时间】:2019-06-07 14:39:48
【问题描述】:
在我们的项目中,我们使用 HTTP 请求通过 Node 调用 REST Web 服务。当我们将它登录到控制台时,我们可以看到结果。但我们想在 HTTP 请求之外存储或使用它。我们怎样才能做到这一点?
var http = require('http');
var options = {
host: 'http:\\example.com',
port: 80,
path : '/site/status?tag=A7351&date=09JAN&name=LASTNAME',
method : 'GET'
};
var result;
http.request(options, function(res){
var body = '';
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
result = JSON.parse(body);
console.log(result, "INSIDE"); //Shows the JSON result
});
}).end();
console.log(result, "OUTSIDE"); //Shows Undefined
结果格式如下:
{
error: 0,
statut: 'STATUTTTTT',
origine: 'PLACEEE',
destination: 'PARIS',
onwardDate: null
}
我们需要能够在 HTTP 调用之外获得结果。
【问题讨论】:
-
感谢您的回复@jonrsharpe。作为一个新手不知道如何使用异步调用。你能指导我完成它吗?这是我在开发中的第一个项目......
标签: node.js json heroku httprequest dialogflow-es