【发布时间】: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