通过 node.js 访问 JSON API

Accessing JSON API via node.js

我想在

上访问 JSON
"https://blockchain.info/tx-index/"+txs[i]+"?format=json"

但每次我尝试使用请求、http 或 https 模块访问它时,我似乎永远无法获得任何信息。永远不会调用回调,据我所知,该函数没有 return 任何东西。

既然我在 javascript 显然是个白痴,你能帮帮我吗?

编辑:我也尝试了这里描述的另一种方法,但它似乎也不起作用。它总是 return 未定义。

function httpGet(url)   {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET",url,false);
    xmlHttp.send();
    return xmlHttp.resonpose;
}

编辑:完整循环代码:

        index = 0;
        txs = info.x.txIndexes;
        for (i = 0; i < txs.length; i++)    {
            console.log(i);
            request.get("https://blockchain.info/tx-index/"+txs[i]+"?format=json&cors=true", function(error,response,body)  {
                console.log("body");
                txs[index] = JSON.parse(body);
                index++;
            });
            var time = Date.now() + 1000;
            while (Date.now() < time)
                var a = true;
            txs[i] = txt;
        }
function httpGet(url)   {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET",url,false);
    xmlHttp.send();
    return xmlHttp.resonpose;
}

resonpose 拼写为 response, but the more generally useful property name is responseText.


您尝试使用 request.get 失败的原因完全不同,但 this question 涵盖了这一点,因为您的加载事件处理程序对 i 使用了错误的值。