无法访问对象属性

时间:2016-01-05 02:11:33

标签: javascript jquery ajax api

我试图访问一个对象属性,但没有任何东西出现,我试图以不同的方式访问它,唯一真正回撤任何东西的是我称之为“列表”。

这是我的代码。

var newP = $("<p>")
var userDate = ("#userDate")
var bitcoinApiUrl = "https://crossorigin.me/https://api.bitcoincharts.com/v1/markets.json";

$(document).ready(function(){
    $(".btn").on("click", function(){
        $.ajax({
            type: "GET",
            url: bitcoinApiUrl,
            success: function(currency) {
                // parse currency
                currency = JSON.parse(currency);

                // loop through currency
                for(var i = 0; i < currency.length; i++) {
                    console.log(currency[i]); //this is the object
                    //use dot notation to get object values
                    var volume = currency[i].volume,
                    latestTrade = currency[i].latest_trade,
                    bid = currency[i].bid,
                    high = currency[i].high,
                    currencyString = currency[i].currency;

                    // Create a string to log
                    var list = "Volume:"+volume+"\n"+"Latest Trade: "+latestTrade+"\n"+"Bid: "+bid+"\n"+"High: "+high+"\n"+"Currency: "+currencyString+"\n"+"\n"+"\n";
                    var currencyList = "Currency: "+currencyString+"\n"+"\n"+"\n";
                    // log the string
                    $(document).ready(function() {
                        $(".modalinfo").append(list.currency);
                        console.log(list["currency"]);

                    })
                }
            }
        });
    });
});

2 个答案:

答案 0 :(得分:2)

以下代码正常。请解释一下你正在尝试做什么。

var bitcoinApiUrl = "https://crossorigin.me/https://api.bitcoincharts.com/v1/markets.json";
$.ajax({
      type: "GET",
      url: bitcoinApiUrl,
      dataType: "json",
      success: function(currency) {
        // loop through currency
        for (var i = 0; i < currency.length; i++) 
        {
          if(currency[i].currency == "USD")
          {
              var $tr = $("<tr />");
              $tr.append( $("<td />").text(currency[i].volume) );
              $tr.append( $("<td />").text(currency[i].latest_trade) );
              $tr.append( $("<td />").text(currency[i].bid) );
              $tr.append( $("<td />").text(currency[i].high) );
              $tr.append( $("<td />").text(currency[i].currency) );
                     
              $("#theTable tbody").append($tr);
          }
        }
      }
  });
#theTable
{
border-collapse: collapse;  
}

#theTable td
{
border: solid 1px black;padding:2px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<table id="theTable">
    <thead>
        <tr><td>Volume</td><td>Latest</td><td>Bid</td><td>High</td><td>Currency</td>
        </tr>
    </thead>
    <tbody></tbody>
</table>

答案 1 :(得分:1)

您的问题很难理解,但我认为这正是您所寻找的:

var list = {volume: volume, latest_trade: latestTrade, bid: bid, high: high, currency: currencyString};
var currencyList = "Currency: "+currencyString+"\n"+"\n"+"\n";
$(".modalinfo").append(list.currency);
console.log(list.currency);

JSFiddle