【发布时间】:2018-01-29 05:19:39
【问题描述】:
我从一个 url 发出一个 get 请求,并尝试在另一个函数中进一步使用响应,所以这是我首先尝试的。
var request = require("request");
function getJSON(getAddress) {
request.get({
url: getAddress,
json: true,
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
return body;
}
})
}
function showJSON(getAddress, callback) {
var test = callback(getAddress);
console.dir(test);
}
showJSON('http://api.open-notify.org/astros.json', getJSON);
然而,当我运行我的脚本时
node ./test.js
我明白了
'undefined' as a console message
我不知道这可能来自哪里,因为我是 node.js、javascript 的新手
【问题讨论】:
-
正如我所说,我完全不确定这是从哪里来的。我会检查这个,希望它能回答我的问题。谢谢
标签: javascript node.js callback get-request