A Pen by liuxiaomingskm on CodePen.
Last active
February 1, 2020 17:21
-
-
Save liuxiaomingskm/ba94b980a79ae5a5185bbc93110796d1 to your computer and use it in GitHub Desktop.
AJAX Bitcoin (练习了bitcoin api的使用和XMLHttpRequest的调用)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Bitcoin Current Price is: <span id="price"></span></h1> | |
<button id="btn">Refresh Data</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var btn = document.querySelector("#btn"); | |
var span = document.querySelector("#price"); | |
//listen for click | |
btn.addEventListener("click", function(){ | |
//make the request | |
var XHR = new XMLHttpRequest(); | |
XHR.onreadystatechange = function(){ | |
if (XHR.readyState == 4 && XHR.status == 200){ | |
var currency = JSON.parse(XHR.responseText).bpi.CNY.code; | |
var rate = JSON.parse(XHR.responseText).bpi.CNY.rate; | |
console.log(currency, rate); | |
span.innerHTML = rate + " " + currency ; | |
} | |
} | |
XHR.open("GET","https://api.coindesk.com/v1/bpi/currentprice/CNY.json"); | |
XHR.send(); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
span { | |
color: green; | |
} | |
button { | |
border: 2px blue solid; | |
border-radius: 5px; | |
color: #0033ff; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment