Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法读取属性';搜索';未定义的Youtube API的定义_Javascript_Youtube - Fatal编程技术网

Javascript 无法读取属性';搜索';未定义的Youtube API的定义

Javascript 无法读取属性';搜索';未定义的Youtube API的定义,javascript,youtube,Javascript,Youtube,在尝试使用JS与Youtube API交互时,我似乎遇到了这个错误。这是我收到的控制台错误 Uncaught TypeError: Cannot read property 'search' of undefined at HTMLFormElement.<anonymous> (app.js:7) at HTMLFormElement.dispatch (jquery-2.2.4.min.js:3) at HTMLFormElement.r.handle (jquery-2.2.4

在尝试使用JS与Youtube API交互时,我似乎遇到了这个错误。这是我收到的控制台错误

Uncaught TypeError: Cannot read property 'search' of undefined
at HTMLFormElement.<anonymous> (app.js:7)
at HTMLFormElement.dispatch (jquery-2.2.4.min.js:3)
at HTMLFormElement.r.handle (jquery-2.2.4.min.js:3)
Uncaught TypeError:无法读取未定义的属性“search”
在HTMLFormElement。(app.js:7)
在HTMLFormElement.dispatch(jquery-2.2.4.min.js:3)
位于HTMLFormElement.r.handle(jquery-2.2.4.min.js:3)
这是我用来交互的脚本。

    function tplawesome(e,t){res=e;for(var n=0;n<t.length;n++){res=res.replace(/\{\{(.*?)\}\}/g,function(e,r){return t[n][r]})}return res}

$(function() {
    $("form").on("submit", function(e) {
       e.preventDefault();
       /* prepare api request */
       var request = gapi.client.youtube.search.list({
            part: "snippet",
            type: "video",
            q: encodeURIComponent($("#search").val()).replace(/%20/g, "+"),
            maxResults: 3,
            order: "viewCount",
            publishedAfter: "2015-01-01T00:00:00Z"
       }); 
       /* execute the request */
       request.execute(function(response) {
          var results = response.result;
          $("#results").html("");
          $.each(results.items, function(index, item) {
            $.get("tpl/item.html", function(data) {
                $("#results").append(tplawesome(data, [{"title":item.snippet.title, "videoid":item.id.videoId}]));
            });
          });
          resetVideoHeight();
       });
    });

    $(window).on("resize", resetVideoHeight);
});

function resetVideoHeight() {
    $(".video").css("height", $("#results").width() * 9/16);
}

function init() {
    gapi.client.setApiKey("myapikey");
    gapi.client.load("youtube", "v3", function() {
        /* check if api is ready */
    });
}

function tplawesome(e,t){res=e;for(var n=0;n您的问题似乎是缺少对
init
方法的调用。在
$(“表单”)上方添加行
init();
。在(“提交”,function(e){…


我尝试了这个,收到了一个未经授权的异常,正如你所期望的那样,来自dud API密钥。

嗨,你从哪里引用了
gapi
?YouTube对象似乎没有定义。API访问密钥是否具有正确的凭据?在Google控制台上创建新应用程序时需要设置此项。它位于底部对于我的HTML,很抱歉。它肯定也是在谷歌控制台中设置的,这让我很困惑。问题可能与
client.load
是异步的,并且在API未准备好时调用
search
调用有关。它看起来也不会调用
init
。如果在第l页调用此函数加载你应该很好,因为搜索直到表单提交才运行。你能给我一个例子吗?对不起,除了JS是我作业的一部分之外,它真的一点都不好。你能发布HTML吗?Uncaught ReferenceError:init没有定义这里是我的例子,直到未经授权的错误:这就是我的原因。Uncaught ReferenceError:init未在HTMLDocument.J(jquery-2.2.4.min.js:2)的Function.ready(jquery-2.2.4.min.js:2)的Object.fireWith[as resolved with](jquery-2.4.min.js:2)的i(jquery-2.2.4.min.js:2)中定义