简体   繁体   English

Coinmarketcap API不起作用

[英]Coinmarketcap API Not Working

Below is an API that I have for coinmarketcap.com. 以下是我为coinmarketcap.com提供的API。 I am trying to pull the current price and place that into a table. 我正在尝试提取当前价格并将其放入表格中。 For some reason this code is not working and I can not figure out the error. 由于某种原因,此代码无法正常工作,我无法找出错误。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
  <table>
    <tr>
      <th>Coin Price</th>
    </tr>
    <td id="my_cell"></td>
  </table>

  <script>
    $.get("https://api.coinmarketcap.com/v1/ticker/", function(data, status) {                  
      for (var i = 0; i < data.length - 1; i++) {                    
        if (data[i].id == "pivx") {                          
          $("#my_cell").innerHTML = data[i].price_usd;                    
        }                  
      }            
    }    
  </script>
</body>
</html>

Your code has a couple of problems. 您的代码有两个问题。 Firstly, your JS is missing a closing ) on the $.get method. 首先,您的JS缺少$.get方法的结尾) You should remember to format your code correctly, then it's almost impossible to miss syntax errors like that. 您应该记住正确格式化代码,然后几乎不可能错过这样的语法错误。 Secondly, the td should be wrapped in a <tr> in the HTML. 其次,应将td包装在HTML的<tr>中。

The main issue however is that you're attempting to use innerHTML on a jQuery object when they do not have that property. 然而,主要的问题是,你试图使用innerHTML jQuery对象时,他们没有这样的属性。 Instead use html() , like this: 而是使用html() ,如下所示:

 $.get("https://api.coinmarketcap.com/v1/ticker/", function(data, status) { for (var i = 0; i < data.length - 1; i++) { if (data[i].id == "pivx") { $("#my_cell").html(data[i].price_usd); } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <th>Coin Price</th> </tr> <tr> <td id="my_cell"></td> </tr> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM