我有一个工作正常的ajax json脚本,但是在控制台中出现错误。
//load youtube videos to game page
function loadGameVideos(){
var formData = {
'game_platform_link' : $('.game_platform_link').attr('name'),
'game_title_link' : $('.game_title_link').attr('name')
};
$.ajax({
type: 'post',
url: '/games/videos_get.php',
data : formData,
dataType : 'json',
beforeSend: function(){
$(".bbox_content.bbox_videos").append("<div class='content_loader_container'><span class='content_loader'><img src='/images/loader.gif'></div>");
},
success: function(viddata) {
$.each(viddata.videos, function(i, video){
$.getJSON("https://www.googleapis.com/youtube/v3/videos", {
key: "AIzaSyAQ26GN-L4JyAcuwOdWRgLLvGNG1uh5g6k",
part: "snippet,contentDetails,statistics",
id: video
}, function(data) {
var duration = convert_time(data.items[0].contentDetails.duration);
var views = data.items[0].statistics.viewCount.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
$(".bbox_content.bbox_videos .content_loader_container").remove();
$(".bbox_content.bbox_videos").append($('<div class="bbox_item bbox_video_item"><a class="video_item youtube" href="/video/' + video + '" style="background-image:url('+ data.items[0].snippet.thumbnails.high.url +');"><span class="img"><img width="100%" src="/images/movie_arrow.png"/></span><span class="video_time">'+ duration +'</span></a><div class="bbox_video_item_info"><span class="video_title">'+ data.items[0].snippet.title +'</span><span class="view_count">Views: '+ views +'</span></div></div>').hide().fadeIn(800));
});
});
}
});
}
错误如下。
未捕获的TypeError:无法读取属性&#39; contentDetails&#39;未定义的
一切正常,我只是不喜欢错误大声笑。谁知道为什么?
谢谢!
答案 0 :(得分:0)
//if you test get json call
$.getJSON("https://www.googleapis.com/youtube/v3/videos", {
key: "AIzaSyAQ26GN-L4JyAcuwOdWRgLLvGNG1uh5g6k",
part: "snippet,contentDetails,statistics",
id: 'G_0TziI12SE'
}, function (data) {
console.log(data.items[0].contentDetails);
});
//contentDetails success returned value
//check your video id callback on this line 'id: video'