如何阅读此API并从中获取信息? 到javascript。
https://bitcoinfees.earn.com/api/v1/fees/recommended
this does not seem to work -
loadJSON("https://bitcoinfees.earn.com/api/v1/fees/recommended", gotData, 'jsonp');
function gotData(data) {
println(data);
}
答案 0 :(得分:1)
loadJSON
不是本机JS函数(它位于库p5中)。您可以像这样使用fetch
函数:
fetch("https://bitcoinfees.earn.com/api/v1/fees/recommended") // Call the fetch function passing the url of the API as a parameter
.then((resp) => resp.json()) // Transform the data into json
.then(function(data) {
// Your code for handling the data you get from the API
console.log(data);
console.log(data.fastestFee); //And since it is just an object you can access any value like this
})
.catch(function(err) {
// This is where you run code if the server returns any errors (optional)
console.log(err)
});
答案 1 :(得分:1)
仅使用javascript:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
// Typical action to be performed when the document is ready:
document.getElementById("demo").innerHTML = xhttp.responseText;
alert(JSON.stringify(xhttp.responseText));
//To display fastestFee's value
var parseData = JSON.parse(xhttp.responseText);
alert(parseData.fastestFee);
}
};
xhttp.open("GET", "https://bitcoinfees.earn.com/api/v1/fees/recommended", true);
xhttp.send();
<div id="demo"> </div>
答案 2 :(得分:0)
Google的第一个结果。学习它。使用它。
var url = "https://bitcoinfees.earn.com/api/v1/fees/recommended";
var req = $.ajax(url, {
success: function(data) {
console.log(data);
},
error: function() {
console.log('Error!');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>