Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
674 views
in Technique[技术] by (71.8m points)

javascript - Youtube Data Api - Uncaught TypeError: Cannot read property 'setApiKey' of undefined

I search music with youtube data api. I use javascript and jquery and i have a problem.
Here is my code

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="<?php echo SITE_PUBLIC; ?>/bootstrap-3.2.0/dist/js/bootstrap.js"></script>
<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>

<script>
function keyWordsearch(){
    gapi.client.setApiKey('myapikey');
    gapi.client.load('youtube', 'v3', function() {
            data = jQuery.parseJSON( '{ "data": [{"name":"eminem"},{"name":"shakira"}] }' );
            $.each(data["data"], function( index, value ) {
                makeRequest(value["name"]);
            });

    });
    }
    function makeRequest(q) {
        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;  
                vidThumburl =  item.snippet.thumbnails.default.url;                 
                vidThumbimg = '<pre><img id="thumb" src="'+vidThumburl+'" alt="No  Image Available." style="width:204px;height:128px"></pre>';                   

                $('#results').append('<pre>' + vidTitle + vidThumbimg +  '</pre>');                      
        })  
    })  
}   
keyWordsearch();
</script>

This code not working. Chrome console say "Uncaught TypeError: Cannot read property 'setApiKey' of undefined". But this code is working:

keyWordsearch() to

$(document).click(function(){
        keyWordsearch()
})

I do not understand this issue. Thanks in advance

EDIT
My code run on jsFiddle.But not run my html file. My html file is here:

<!doctype html>
<html>
  <head>
  <title>Search</title>


  </head>
  <body> 
    <div id="container">
      <h1>Search Results</h1>
      <ul id="results"></ul>
    </div>        
 <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>   
    <script>
     $(function(){
     function keyWordsearch(){
        gapi.client.setApiKey('AIzaSyCWzGO9Vo1eYOW4R4ooPdoFLmNk6zkc0Jw');
        gapi.client.load('youtube', 'v3', function() {
                data = jQuery.parseJSON( '{ "data": [{"name":"eminem"}] }' );
                $.each(data["data"], function( index, value ) {
                    makeRequest(value["name"]);
                });

        });
}
    function makeRequest(q) {
        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;  
                vidThumburl =  item.snippet.thumbnails.default.url;                 
                vidThumbimg = '<pre><img id="thumb" src="'+vidThumburl+'" alt="No  Image Available." style="width:204px;height:128px"></pre>';                   

                $('#results').append('<pre>' + vidTitle + vidThumbimg +  '</pre>');                      
        })  
    })  
}
    keyWordsearch();
     })
  </script> 


</body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looks like, you haven't load the javascript library. That's why it can't find the reference. You can add it like:

<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...