json数据错误未捕获TypeError:无法将undefined或null转换为对象

时间:2018-02-13 22:33:06

标签: javascript json

如何查询此https://api.iextrading.com/1.0/stock/aapl/book数据。

我想抓住开放的,可能是其他一些数据并将其传递给它。

setInterval(function(){
    $.ajax({
    url: 'https://api.iextrading.com/1.0/stock/aapl/book',
    dataType: 'json',
    type: 'get',
    cache: false,
    success: function(data){
    $(Object.keys(data['close'])).each(function(index, key) {
      document.getElementById("stockprice").innerHTML = (data['close'][key].quote);
    });
    }
    });
}, 10000); 

错误:

Uncaught TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at Object.success ((index):84)
    at u (jquery.js:2)
    at Object.fireWith [as resolveWith] (jquery.js:2)
    at k (jquery.js:2)
    at XMLHttpRequest.<anonymous> (jquery.js:2)

1 个答案:

答案 0 :(得分:0)

对于数据似乎不是close属性,您可能希望从引用属性中获取此属性,如下所示:

setInterval(function(){
    $.ajax({
        url: 'https://api.iextrading.com/1.0/stock/aapl/book',
        dataType: 'json',
        type: 'get',
        cache: false,
        success: function(data){
            document.getElementById("stockprice").innerHTML = data.quote.close;
        }
    });
}, 10000);