javascript - Can not get data from AJAX request to the Coinhive API
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.
Can anyone please help me?
Thanks in common, Dennis.
edit:
I tried the following:
<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>
And I also tried:
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?
Answer
Solution:
figured it out!
The JSON request:
The postback to the end user:
I decided to go with php and kind of ditch the html getjson part.
Answer
Solution:
Client side requests to the Coinhive HTTP API are restricted by CORS rules:
From API documentation: