【问题标题】:empty results from YouTube API requestYouTube API 请求的空结果
【发布时间】:2018-03-29 14:52:16
【问题描述】:

我不明白为什么 items 数组是空的。有人可以告诉我什么不起作用吗?谢谢。

jQuery(document).ready(function ($) {

$('#infl-yt-label').on('click', function() {
    //$('#infl-inp-search').attr("placeholder","YouTube input...");
    $('#infl-form-ig').hide();
    $('#infl-form-yt').show();
});

$('#infl-it-label').on('click', function() {
    //$('#infl-inp-search').attr("placeholder","Instagram input...");
    $('#infl-form-yt').hide();
    $('#infl-form-ig').show();
});

 $("#searchchannels").on("click", function() {
    console.log($("#infl-inp-search-yt").val());
    $.get(
        "https://www.googleapis.com/youtube/v3/search?",
        {
            part:"snippet",
            type:"channel",
            q: encodeURIComponent($("#infl-inp-search-yt").val()).replace(/%20/g, "+"),
            key:key
        },
             function(data)
             {
                console.log(data.items)
                 $.each(data.items, function()
                 {
                 console.log(data.items);
                 })
             }

        );//end get
    alert(1);
});//end 

});// end

html:

        <div class="infl-sociale col-md-3 col-lg-2 text-center">
            <input id="infl-yt" type="radio" name="infl-sociale" value="youtube" checked>
            <label for="infl-yt" id="infl-yt-label"><i class="fa fa-youtube-play" aria-hidden="true"></i></label>
            <input id="infl-it" type="radio" name="infl-sociale" value="instagram">
            <label for="infl-it" id="infl-it-label"><i class="fa fa-instagram" aria-hidden="true"></i></label>
        </div>   

注意:我在标题中没有指向 google api 链接的脚本链接。

【问题讨论】:

  • 回调时可能出现异步错误或回调未解析为 JSON。
  • 您没有在each 函数中传递数据,只需添加$.each(data.items, function(data)
  • @Grasper,我试过你的解决方案,它不起作用。

标签: javascript jquery ajax youtube-api


【解决方案1】:

您应该更新函数以正确迭代“items”集合。

function(data)
{
    console.log(data.items) /* what does this log to the console? */
    $.each(data.items, function(item)
    {
        console.log(item);
    })
}

话虽如此,第一个console.log(data.items) 的结果是什么?如果这是空的,迭代它不会做任何事情。您也可以尝试 console.log(data) 或查看 Chrome 调试网络选项卡以查看 API 调用的响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 2019-05-16
    • 2020-04-22
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    相关资源
    最近更新 更多