通过node.js访问JSON API

时间:2015-08-02 20:04:11

标签: javascript json node.js bitcoin

我想在

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

但每次我尝试使用请求,http或https模块访问它时,我似乎永远无法获得任何信息。永远不会调用回调,并且尽可能接近,函数不会返回任何内容。

因为我在javascript中显然是个白痴,你能帮助我吗?

编辑:我也尝试过这里描述的另一种方法,但它似乎也没有用。它总是返回undefined。

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;
        }

1 个答案:

答案 0 :(得分:1)

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

resonpose的拼写为response,但更常用的属性名称为responseText

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