2016-12-06 134 views
0
<!doctype html> 
<html> 
<head> 
<title>Search</title> 
    </head> 
    <body> 
    <div id="buttons"> 
    <label> <input id="query" type="text"/><button id="search-button" onclick="keyWordsearch()">Search</button></label> 



    <div id="container"> 
     <h1>Search Results</h1> 
     <ul id="results"></ul> 
    </div>   

    <script> 
    function keyWordsearch(){ 
     gapi.client.setApiKey("AIzaSyCFdNzrcERfUCd1R5rk1h3JAjXNT9svi5g"); 
     gapi.client.load('youtube', 'v3', function() { 
       makeRequest(); 
     }); 
      } 

     function makeRequest() { 
     var q = $('#query').val(); 
     var request = gapi.client.youtube.search.list({ 
       q: q, 
       part: 'snippet', 
       maxResults: 10 
     }); 
     request.execute(function(response) {                      
       $('#results').empty() 
       var srchItems = response.result.items;      
       $.each(srchItems, function(index, item) { 
       vidTitle = item.snippet.title; 

       $('#results').append('<pre>' + vidTitle + '</pre>');      
     }) 
    }) 
} 
    </script> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
    <script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"> </script> 
    </body> 
</html> 

所以我想要做的就是使用YouTube上使用Youtube API的关键字搜索视频。但是,youtube页面上提供的示例也需要“身份验证”。我需要在没有认证的情况下完成,只需使用API​​密钥即可。现在发生的事情是,当我按搜索按钮时,它什么也不做。我一直试图弄清楚自己,但我没有发现任何其他问题与我正在经历......帮助...Javascript搜索视频使用YouTube API通过API密钥

+0

它正在为我工​​作没有任何问题,检查此 - http://prntscr.com/dfu9wn –

回答

0

您的代码是完全正常工作我的朋友。我运行了你的代码,这里是screenshot

所以我所做的就是:

  1. 使用 '蟒蛇-m SimpleHTTPServer 8000' 在Linux上
  2. // IM然我在Python服务器又到浏览器中,打开本地主机,位于我的search.html文件其中包含您的代码
  3. 瞧,它的工作原理。