【发布时间】:2022-11-11 14:53:54
【问题描述】:
我目前正在尝试制作自己的加密货币监视列表。我正在使用 CoinRankingAPI (https://developers.coinranking.com/api/documentation/coins)。我试图遍历硬币数据以检索价格,但我收到一个错误,指出我试图迭代的对象不是易怒的。源代码将在下面。任何帮助将不胜感激!
const key = 'XXXX';
const url = 'https://api.coinranking.com/v2/coins?' + key;
//Loads Stats Data
function loadPrices() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var coins = JSON.parse(this.responseText);
// get 'data' key inside response
var price = coins.data;
// loop all the teams
for (var coin of price) {
// print full name and abbreivation
document.getElementById("hello").innerHTML += "<br />" + coin["price"] ;
}
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
【问题讨论】:
-
如果它不是可迭代的,那么它就不是一个数组。那是什么?调试时,您在
price中观察到什么值?你期望它有什么价值?为什么? -
您可以发布
this.responseText的值吗? -
根据 api 文档,我相信您想要
coins.data.coins -
请提供您从外部 API 获得的数据的摘录,以便您的问题的读者不必不必要地重复您已经完成的研究和/或可能注册随机网站。由于您是寻求帮助调试,因此您需要提供minimal reproducible example
-
那么你想循环什么?
console.log(price)调试永远不会受到伤害。
标签: javascript html api