无法将数据从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.
我使用所有 mysql 的问题的用户门户设置a>值,但我无法获取json 参与工作.
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.