【问题标题】:Bittrex Api in pure Javascript纯 Javascript 中的 Bittrex Api
【发布时间】: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


【解决方案1】:

Bittrex Api 不允许使用 CORS,因此您无法在浏览器中运行 javascript 代码。你需要找到支持CORS的Api,或者使用服务端程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 2018-05-29
    • 2020-06-18
    相关资源
    最近更新 更多