2016-02-25 66 views
1

我有一個ajax json腳本,工作得很好,但是我在控制檯中出現錯誤。Ajax Json Youtube API錯誤contentDetails

//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)); 
       }); 
      }); 
     } 
    }); 
} 

錯誤如下。

Uncaught TypeError: Cannot read property 'contentDetails' of undefined

一切正常,我只是不喜歡有錯誤哈哈。有人知道爲什麼

謝謝!

回答

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'