简体   繁体   English

无法使用api获取json数据

[英]can't get json data with api

I am using below javascript and html to get the json data. 我正在使用下面的javascript和html来获取json数据。 But i can't make it work with this api.It just return blank result. 但是我无法使其与此API一起使用。它只会返回空白结果。 I have checked the api link is working so i don't think is the server problem. 我检查了api链接是否正常工作,所以我认为这不是服务器问题。 Please help! 请帮忙!

html HTML

   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <div id="balance"></div>

javascript JavaScript的

$(function() {
  $.ajax({
    type: "GET",
    url:"https://xmg.minerclaim.net/index.php?page=api&action=public",
    dataType: "json",
    success: function(data) {
      console.log(typeof data); // -- Object
      var json = data;

      $('#balance').html(json.hashrate);

    }
  });
});

json data json数据

{"pool_name":"minerclaim.net","hashrate":60185.64096,"workers":1056,"shares_this_round":168700,"last_block":1531882,"network_hashrate":61752985,"fee":1,"payout":"prop"}

As I mentioned above, you are getting bool(true) in your PHP output, because the usage of var_dump() . 如前所述,由于var_dump()的使用,您在PHP输出中得到的是bool(true)

An example of a PHP file you could use: 您可以使用的PHP文件示例:

    <?php

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, "https://xmg.minerclaim.net/index.php?page=api&action=public");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 0);

        $headers = array();
        $headers[] = "Content-Type: application/json; charset=utf-8";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Some error:' . curl_error($ch);
            die();
        }

        // You want your browser to think it's JSON, as we return as JSON
        header("Content-Type: application/json; charset=utf-8");
        echo($result);

        curl_close($ch);
?>

You can copy + paste this into a new PHP file, point the $.ajax -url to that new file and see the magic happen. 您可以将其复制+粘贴到新的PHP文件中,将$.ajax -url指向该新文件,然后看魔术发生了。 The example above works for me as I'm getting results back. 上面的示例对我有用,因为我正在获得结果。

As for your javascript, you could also try to add contentType: "application/json", after `dataType: "json",", as this will make sure the content-type of the headers that are returned are set. 至于您的JavaScript,您也可以尝试在`dataType:“ json”,“之后添加contentType: "application/json", “,因为这将确保设置了返回的标头的content-type。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM