【发布时间】:2018-06-16 19:32:51
【问题描述】:
我正在尝试用纯 Javascript 读取 Bittrex 的公共 API。但我无法让它工作。如果我改为调用coinmakretcap api,它确实有效。我想在不使用任何包的情况下异步读取它。有人对如何实现这一点有一些建议吗?
这是我的代码:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p id="demo"></p>
<script>
var api = "https://bittrex.com/api/v1.1/public/getticker?market=BTC-UBQ" ;
//var api = "https://api.coinmarketcap.com/v1/ticker/dash/?convert=EUR";
function apicall(api) {
xhr = new XMLHttpRequest();
xhr.open("GET", api, true);
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//obj = JSON.parse(xhr.responseText);
obj = xhr.responseText;
printanswer(obj);
}
};
xhr.send();
}
function printanswer(obj) {
document.getElementById("demo").innerHTML += obj;
}
apicall(api);
</script>
</body>
</html>
【问题讨论】:
-
这是不可能的,因为 api 端点没有为跨端脚本启用访问控制。
标签: javascript api asynchronous