[英]How do I make an HTTP request to bitstamp?
I'm trying to use an API from bitstamp to fetch a currency trading price on my webpage. 我正在尝试使用bitstamp中的API来获取我的网页上的货币交易价格。
I have researched this problem, but I still cannot get it to work as it always returns ERROR
我已经研究过这个问题,但我仍然无法让它工作,因为它总是返回
ERROR
The link used is https://www.bitstamp.net/api/ticker/
and the response should be last
使用的链接是
https://www.bitstamp.net/api/ticker/
,响应应该是last
Here is my code: 这是我的代码:
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.bitstamp.net/api/ticker/", true);
xhr.send();
xhr.addEventListener("readystatechange", processRequest, false);
function processRequest(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
window.alert(response.last);
}
else {
window.alert("ERROR");
} }
Try this: 尝试这个:
function loadXMLDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { var jsonRes= JSON.parse(this.responseText); if (jsonRes.hasOwnProperty('last')) { document.getElementById("demo").innerHTML = jsonRes.last; alert(jsonRes.last); } } }; xhttp.open("GET", "https://www.bitstamp.net/api/ticker", true); xhttp.send(); }
<h2>Using the XMLHttpRequest object</h2> <button type="button" onclick="loadXMLDoc()">Change Content</button> <p>last attribute is: <span id="demo"></span></p>
Here is one way: 这是一种方式:
<script src="./jquery.min.js">
//none secure web page ?
jQuery.get("https://www.bitstamp.net/api/ticker/", function (data, status)
{
// use response here; jQuery passes it as the first parameter
var response = JSON.parse(data);
window.alert(response.last);
console.log("MyFunc: " + "response : " + response + "\nStatus: " + status);
});
</script>
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.bitstamp.net/api/ticker/", true);
xhr.send();
xhr.addEventListener("readystatechange", processRequest, false);
function processRequest(e) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
window.alert(response.last);
} else {
window.alert("ERROR");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.