gabe appleton
gabe appleton

Reputation: 378

Accessing JSON API via node.js

I'd like to access the JSON on

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

But each time I try to access it with the request, http, or https modules, I can never seem to get any information. The callback is never called, and as near as I can tell, the function doesn't return anything.

Since I'm apparently an idiot in javascript, can you help me out?

Edit: I also tried another method described on here, but it doesn't seem to work either. It always returns undefined.

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

Edit: Full loop code:

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

Upvotes: 1

Views: 62

Answers (1)

Quentin
Quentin

Reputation: 944320

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

resonpose is spelt response, but the more generally useful property name is responseText.


Your attempt to use request.get is failing for a completely different reason, but that is covered by this question as your load event handler is using the wrong value for i.

Upvotes: 1

Related Questions