【问题标题】:How do I get data from the Bitfinex web API using javascript如何使用 javascript 从 Bitfinex Web API 获取数据
【发布时间】:2014-09-18 11:39:56
【问题描述】:

这是他们API documentation的链接

我想通过从头开始创建比特币价格代码来练习 Web 编程。我的计划是提供一个脚本,让 api 调用交易所来显示数据。这意味着我只需要提供脚本,而不是处理数据服务器端。

我知道编程的一部分是从文档中学习,但是来自 bitfinex 的文档非常稀少,我找不到教程。

我创建了一个 index.html 来测试我的 javascript。它返回一个控制台错误:

XMLHttpRequest cannot load https://api.bitfinex.com/v1/pubticker/:last_price. 
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'null' is therefore not allowed access. 

这里是完整的 index.html:

<html>

<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
</head>

<body>
<script>
$.getJSON("https://api.bitfinex.com/v1/pubticker/:last_price",
    function(data, status){
      alert("price: "+data +" status: " + status);
    }
)
</script>
Thank you stack exchange
</body>

【问题讨论】:

  • 您的问题似乎是实现而不是设计(“我如何调试这个”)。因此,它不太适合 P.SE(有关 P.SE 范围内的问题的更多信息,请参阅help center)。

标签: javascript asp.net-web-api


【解决方案1】:

如果您在其上运行带有 SSL 的网络服务器(正如 Duskwuff 所说,“您将需要从运行在您的网络服务器上的脚本调用此 API。”)然后您可以将以下文件部署到它:

<?php
$func = $_GET['fn'];
if(in_array($func,array('getBfx')))
{
    $func();
}

function getBfx()
{
    $a = $_GET['api'];
    echo "objData = ".file_get_contents($a);
}
?>

然后,您可以从部署脚本的 URL 中包含脚本,并在其末尾添加 ?func=getBfx&api=https://api.bitfinex.com/v1{Whatever}。我不必转义该 URL,但您可以。

【讨论】:

    【解决方案2】:

    你不能——至少,不能用 Javascript。该 API 未配置为允许来自其他网站上运行的 Javascript 的调用。您将需要从您的网络服务器上运行的脚本调用此 API。

    (另外,对于它的价值,URL 中的 :symbol 标记应该替换为您尝试查找的代码的符号,例如,/v1/pubticker/BTCUSD 代表 BTC/USD 汇率。)

    【讨论】:

      猜你喜欢
      • 2017-12-19
      • 2021-12-19
      • 2018-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      相关资源
      最近更新 更多