【问题标题】:TypeError: data.data[i].caption is null Instagram apiTypeError: data.data[i].caption is null Instagram api
【发布时间】:2017-08-11 07:51:00
【问题描述】:
$(function() {

    $.ajax({
        type: "GET",
        dataType: "jsonp",
        cache: false,
        url: "https://api.instagram.com/v1/users/self/media/recent/?access_token=",
        success: function(data) {
            for (var i = 0; i < 20; i++) {
                $("#pics").append("<a target='_blank' href='" + data.data[i].link + "'><img src='" + data.data[i].images.low_resolution.url + "'></img></a>");
                $("#text").append("<p>"+data.data[i].caption.text+"</p>");


            }
        }
    });
});

我尝试获取 Instagram 提要 api,代码工作正常,但我有错误 TypeError: data.data[i].caption is null,提要文本未显示所有我在 json 中仅获得 3 个表单 20 元素。

data json link

【问题讨论】:

标签: javascript api instagram


【解决方案1】:

如果您查看响应,您会发现它只是意味着没有标题(例如,查看数字 3)

试试这样的:

for (var i = 0; i < 20; i++) {
    $("#pics").append("<a target='_blank' href='" + data.data[i].link + "'><img src='" + data.data[i].images.low_resolution.url + "'></img></a>");
    if (data.data[i].caption === null) {
        $("#text").append("<p>No caption</p>");
    } else {
        $("#text").append("<p>"+data.data[i].caption.text+"</p>");
    }
}

【讨论】:

    【解决方案2】:

    原因在于响应中的某些标题为空,例如 data[3] ,data[4]。在检索位于 caption 对象内的 text 之前进行空检查

    for (var i = 0; i < 20; i++) {
          if (res.data[i].caption !== null) {
            console.log(res.data[i].caption.text);
          }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-13
      • 2021-11-16
      • 2018-08-23
      • 1970-01-01
      • 2015-03-12
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多