创建YouTube播放器结果 - 未捕获的ReferenceError:YT未定义错误

时间:2016-11-04 07:16:17

标签: javascript api google-api youtube-api

为什么我收到此错误?我正在创建一个通过YT api嵌入的youtube播放器,但我一直收到一个警告,即没有定义变量YT。我可以看到youtube API的脚本被包含在内,它应该创建变量YT - 所以为什么这不起作用?

和我的代码:     

    //submit button to call getRequest
    $('#search-term').submit(function(event){
        event.preventDefault();
        var searchTerm = $('#query').val();
        test = getRequest(searchTerm);
        console.log("test=");
        console.log(test);
    });
    function getRequest(searchTerm){
        var params = {
            part:'snippet',
            key: "<removed from code to post>",
            q:searchTerm
          };
        url = 'https://www.googleapis.com/youtube/v3/search';

        $.getJSON(url, params, function(data){
            var returnedVideos = data.items;

            // Let's loop over the thisVideobers in our array
            for (var i = 0; i < returnedVideos.length; i++) {
                // This is the object (returned by searchResult) we're on...
                var thisVideo = returnedVideos[i];
                var thisVideoURL = thisVideo.snippet.thumbnails.medium.url;
                var thisVideoID = thisVideo.id.videoId;

                // We're creating a DOM element for the object
                var elem = document.createElement('img');
                elem.content = thisVideo;
                elem.setAttribute("id", thisVideoID);
                elem.setAttribute("src", thisVideoURL);
                console.log(elem.id);

                // ... and when we click, load the respective video
                elem.addEventListener('click', (function(clickEvent) {
                    return function() {
                        console.log('clickEvent=');
                        console.log(clickEvent);
                        console.log(clickEvent);
                        var YoutubeVideoID = $(this).attr('id');

                        // Load the IFrame Player API code asynchronously.
                        var tag = document.createElement('script');
                        tag.src = "https://www.youtube.com/player_api";
                        var scriptTag = document.getElementsByTagName('script')[0];
                        scriptTag.parentNode.insertBefore(tag, scriptTag);
                        onYouTubePlayerAPIReady();
                        // Replace the 'ytplayer' element with an <iframe> and
                        // YouTube player after the API code downloads.
                        var player;
                        function onYouTubePlayerAPIReady() {
                            player = new YT.Player('ytplayer', {
                                height : '360',
                                width : '640',
                                videoId : YoutubeVideoID
                            });
                        }
                    };
                })(thisVideo));

                document.body.appendChild(elem);
            }
        });         
    }




</script>

完整错误

  

YoutubeTestPlayer.html:64未捕获的ReferenceError:未定义YT   at onYouTubePlayerAPIReady   (file:/// C:/\ t。/ YoutubeTestPlayer.html?:64:22)at   HTMLImageElement。   (文件:/// C:/.........../YoutubeTestPlayer.html:59:8)

0 个答案:

没有答案