如何从网站的API中获取单个值?

时间:2018-04-12 20:23:23

标签: javascript json api http get

这个网站:

https://turtle-coin.com/#api

进入哈希值时,会显示一个值。我需要捕捉到这个价值,但并不知道如何。

到目前为止,我已经尝试过这个:

function getNetworkData(){
$.getJSON('../json/networks/network-list.json', function(data) {

    data.networks.forEach(function(network){

    var api_url = network.hashrate_url;

        var xmlHttp = new XMLHttpRequest();
        xmlHttp.open("GET", api_url);
        try {
            xmlHttp.send();
            responseText = xmlHttp.responseText;
            var data = JSON.parse(responseText);
        } catch (ex) {
            console.error(ex);
        }

    console.log(data);

        });

});
}

我收到错误:

SyntaxError: Unexpected end of JSON input

有什么想法吗?

修改

这是打印哈希值的代码:

<?php 
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:11898/getinfo');
$result = curl_exec($ch);
$obj = json_decode($result, TRUE);
curl_close($ch);
//print_r($obj);
$difficulty = $obj['difficulty'];
$hashrate = round($difficulty / 30);
print_r($hashrate);
?>

EDIT2

嗯,起初我得到了:

Failed to load https://turtle-coin.com/q/hashrate/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

但我处理了安装名为CORS的Chrome插件。

现在脚本运行正常,但它没有记录任何值。我在控制台中看到一个函数调用,但它只是空的。没有捕捉到价值。

0 个答案:

没有答案