无法将数据从AJAX请求获取到Coinhive API [英] Can not get data from AJAX request to the Coinhive API

查看:169
本文介绍了无法将数据从AJAX请求获取到Coinhive API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试从Json API获取数据并将其发布回html表中. 我尝试通过以下方式执行此操作:https://api.coinhive.com/user/balance?name=username&secret=mysecret 它确实显示了我:{"success":true,"name":"*user*","total":49152,"withdrawn":0,"balance":* a value *}

I recently tried to get the data from a Json API and post that back to a table in html. I tried to do this from: https://api.coinhive.com/user/balance?name=username&secret=mysecret It does show me: {"success":true,"name":"*user*","total":49152,"withdrawn":0,"balance":* a value *}

但是我想让我的用户按下一个按钮,该按钮会将余额值加载到他们自己的用户门户中的表中.

But I want to let my users push a button which will load the balance value to a table in their own user portal.

我使用所有参与工作.

I got the user portal setup with all the mysqlvalues, But i can't manage to get the jsonpart to work.

有人可以帮助我吗?

谢谢大家, 丹尼斯.

我尝试了以下操作:

<html lang="en">
<head>
<title>JavaScript - read JSON from URL</title>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>

<body>
    <div class="mypanel"></div>

    <script>
    $.getJSON('https://api.coinhive.com/user/balance?name=username&secret=mysecret', function(data) {

        var text = `Balance: ${data.balance}<br>`


        $(".mypanel").html(text);
    });
    </script>

</body>
</html>

我也尝试过:

function setup() {
    loadJSON("https://api.coinhive.com/user/balance?name=username&secret=mysecret", gotData);
}

function gotData(data) {
    alert(data);
}

那是行不通的. 我在做什么错了?

And that isn't working. What am I doing wrong?

推荐答案

弄清楚了!

JSON请求:

<?php
$url = 'the api url';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  "Content-Type: application/x-www-form-urlencoded"
));

$data = curl_exec($ch);
curl_close($ch);

$result = json_decode($data, true);

?>

回发给最终用户:

<span id="test" style="color:white"><?php
if (isset($data))
{
   echo $result['balance'];
}
else
{
   echo 'damn';
}
?></span>

我决定使用php并放弃html getjson部分.

I decided to go with php and kind of ditch the html getjson part.

这篇关于无法将数据从AJAX请求获取到Coinhive API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆