使用JavaScript的Exchange API [英] Exchange API using javascript

查看:226
本文介绍了使用JavaScript的Exchange API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaScript在此网站上使用API​​



您不能从JS代码调用API,因为 api.mybitx.com 会阻止跨域请求。



您有几种解决方案:


  1. 创建自己的服务器端API方法。
    从您的JS代码中调用此方法。
    使用api.mybitx.com API system.net.webrequest(v = vs.110).aspx rel = nofollow noreferrer> WebRequest 或 RestSharp


  2. 如果只想在JS代码上执行此操作,则需要使用 EasyXDM 库。


第一种解决方案更简单,更正确。


I'm using javascript to use the API on this site https://www.luno.com/en/api and I don't understand why I am not receiving any data. When if I just enter this url: https://api.mybitx.com/api/1/ticker?pair=XBTZAR I do receive data in the broswer.

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    document.getElementById("luno").innerHTML = myObj.bid;
  }
};
xmlhttp.open("GET", "https://api.mybitx.com/api/1/ticker?pair=XBTZAR", true);
xmlhttp.send();

Why is it not returning anything?

解决方案

You have a trouble with CORS requests.

You can see similar error on your developer console:

You can't call API from JS code because api.mybitx.com blocks cross domain request.

You have a several solutions:

  1. Create own server side API method. Call this method from your JS code. Inside the API method call api.mybitx.com API using WebRequest or RestSharp.

  2. If you want to do it only on JS code, then you need to use EasyXDM library.

For me the first solution is more simpler and more correct.

这篇关于使用JavaScript的Exchange API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆