【发布时间】:2017-05-21 23:43:45
【问题描述】:
/*你好,窥视,
我希望你能帮助我,我正在尝试从同一个 coinmarket URL 获取 3 个不同的代码。我希望能够在同一个脚本中调用蜂蜜币、比特币和游戏积分。不幸的是,这个API似乎没有一个选项可以一次专门调用它们但是有一个选项可以调用它们的巨大列表并以这种方式提取它们,尽管很难确定哪个是哪个(因为我不知道该怎么做)。
-如果您想知道-: Anthropodia 目标是一种“游戏内货币”,由于 stackoverflow 自己的 Fen1x,该脚本的第一次迭代确实有效。 :) 目的是让我们的游戏内货币通过使用加密货币真实地波动。玩家以后可以通过购买加密货币来影响游戏...现在我只是设置网页,反正我尝试了两种不同的方法,
第一个示例没有任何结果,只是一个空白页面,不幸的是,我认为这超出了我的想象。我试图通过调用一组代码并仅使用前 3 个对象“price_usd”来规避这一点,但这不符合最初的计划。那么有没有人对第一个或第二个示例或两者都有答案?
第一次尝试:
我很乐意将您的用户名或真实姓名(如果您愿意)添加到游戏积分和我们的网页上,任何有兴趣加入我们游戏项目的人都非常受欢迎(这完全是自愿的,我是唯一的编码员有了javascript知识,我也拉了短稻草。:)
*/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" Access-Control-Allow-Origin: *>
<link href="style.css" type="text/css" rel="stylesheet">
<head>
<title>Displaying JSON Data</title>
</head>
<script type="text/javascript">
window.addEventListener('load',init,false);
function init(e) {
document.getElementById('myButton').addEventListener('click',documentLoader,false);
}
function reqListener() {
var obj = JSON.parse(this.responseText);
document.getElementById('honey_price_usd').innerHTML= obj[0].price_usd; //Honey Coin Price
document.getElementById('Anthropodia Honey Price').innerHTML= obj[0].price_usd * 10; //Anthropodia Honey Price * 10
}
function reqListener() {
var obj = JSON.parse(this.responseText);
document.getElementById('game_price_usd').innerHTML= obj[0].price_usd; //Game Credits Price
document.getElementById('Anthropodia Sugar Price').innerHTML= obj[0].price_usd * 10; //Anthropodia Sugar Price * 10
}
function reqListener() {
var obj = JSON.parse(this.responseText);
document.getElementById('btc_price_usd').innerHTML= obj[0].price_usd; //Bitcoin Price
document.getElementById('Anthropodia Asparatime Price').innerHTML= obj[0].price_usd * 10; //Anthropodia Asparatime Price * 10
}
function documentLoader(){
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("GET", "https://api.coinmarketcap.com/v1/ticker/HONEY/?convert=USD",true);
oReq.send();
}
function documentLoader(){
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("GET", "https://api.coinmarketcap.com/v1/ticker/gamecredits/?convert=USD",true);
oReq.send();
}
function documentLoader(){
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("GET", "https://api.coinmarketcap.com/v1/ticker/bitcoin/?convert=USD",true);
oReq.send();
}
</script>
<body>
<!-- Form that holds Anthropodia Currency Prices -->
<form id="Currency_Form" style="width:50%">
<table style="width:100%">
<center> <h1>Anthropodia Currency</h1> </center>
<tr>
<th>Crypto-Currency</th>
<th>Game Currency</th>
</tr>
<tr>
<td>Honey Coin Price: <div id="honey_price_usd"></div></td>
<td>Anthropodia Honey Price: <div id="Anthropodia Honey Price"></div></td>
</tr>
<tr>
<td>Game Credits Price: <div id="game_price_usd"></div></td>
<td>Anthropodia Sugar Price: <div id="Anthropodia Sugar Price"></div></td>
</tr>
<tr>
<td>Bitcoin Price: <div id="btc_price_usd"></div></td>
<td>Anthropodia Asparatime Price: <div id="Anthropodia Asparatime Price"></div></td>
</tr>
</table>
<center>
<button id="myButton" type="button">Click to load</button>
<center>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" Access-Control-Allow-Origin: *>
<link href="style.css" type="text/css" rel="stylesheet">
<head>
<title>Displaying JSON Data</title>
<script type="text/javascript">
window.addEventListener('load',init,false);
function init(e) {
document.getElementById('myButton').addEventListener('click',documentLoader,false);
}
function reqListener() {
var obj = JSON.parse(this.responseText);
document.getElementById('honey_price_usd').innerHTML= obj[0].price_usd; //Honey Coin Price
document.getElementById('Anthropodia Honey Price').innerHTML= obj[0].price_usd * 10; //Honey Coin Price * 10
document.getElementById('game_price_usd').innerHTML= obj[1].price_usd; //Game Credit Price
document.getElementById('Anthropodia Sugar Price').innerHTML= obj[1].price_usd * 10; //Sugar Coin Price * 10
document.getElementById('btc_price_usd').innerHTML= obj[2].price_usd; //Bitcoin Price
document.getElementById('Anthropodia Asparatime Price').innerHTML= obj[2].price_usd * 10; //Asparatime Coin Price * 10
}
function documentLoader(){
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("GET", "https://api.coinmarketcap.com/v1/ticker/?limit=10",true);
oReq.send();
}
/* Thanks go out to Fen1x from Stackoverflow for helping fix the code (you are also on the webpage) */
</script>
</head>
<body>
<!-- Form that holds Anthropodia Currency Prices -->
<form id="Currency_Form" style="width:50%">
<table style="width:100%">
<center> <h1>Anthropodia Currency</h1> </center>
<tr>
<th>Crypto-Currency</th>
<th>Game Currency</th>
</tr>
<tr>
<td>Honey Coin Price: <div id="honey_price_usd"></div></td>
<td>Anthropodia Honey Price: <div id="Anthropodia Honey Price"></div></td>
</tr>
<tr>
<td>Game Credits Price: <div id="game_price_usd"></div></td>
<td>Anthropodia Sugar Price: <div id="Anthropodia Sugar Price"></div></td>
</tr>
<tr>
<td>Bitcoin Price: <div id="btc_price_usd"></div></td>
<td>Anthropodia Asparatime Price: <div id="Anthropodia Asparatime Price"></div></td>
</tr>
</table>
<center>
<button id="myButton" type="button">Click to load</button>
<center>
</form>
</body>
</html>
【问题讨论】:
-
没有接受者嗯......好吧,如果我自己解决这个问题,我一定会发布它。
标签: javascript json xmlhttprequest