通过node.js访问JSON API

Accessing JSON API via node.js

本文关键字:JSON API 访问 js node 通过      更新时间:2023-09-26

我想访问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;
        }
function httpGet(url)   {
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET",url,false);
    xmlHttp.send();
    return xmlHttp.resonpose;
}

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


您尝试使用request.get失败的原因完全不同,但这是由这个问题所涵盖的,因为您的加载事件处理程序正在使用i的错误值。