2016-12-06 133 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. 瞧,它的工作原理。