0

I'm trying to display data from a Google API, but keep getting "undefined" as my results. Here is my code:

$(document).ready(function() {
var url='https://www.googleapis.com/books/v1/volumes?q=harry+potter&maxResults=9'
    $('#button').click(function() {
        $.getJSON(url,function(data){
            $('#content').empty();
            $.each(data, function(entryIndex, entry){
                var html = '<div class="result">';                    
                html += '<h3>' + entry['id'] + '</h3>';
                html += '<div class="title">' + entry['title'] + '</div>';                  
                html += '<div class="author">' + entry['author'] + '</div>';                                        
                $('#content').append(html);
            });                        
        });
        return false;
    });
});

1 Answer 1

2

Your code should look as follows to get the information you desire for the API:

$(document).ready(function() {
var url='https://www.googleapis.com/books/v1/volumes?q=harry+potter&maxResults=9'
$('#button').click(function() {
    $.getJSON(url,function(data){
        $('#content').empty();
        $.each(data.items, function(entryIndex, entry){
            var html = '<div class="result">';                    
            html += '<h3>' + entry.id + '</h3>';
            html += '<div class="title">' + entry.volumeInfo.title + '</div>';                  
            html += '<div class="author">' + entry.volumeInfo.authors + '</div>';                                        
            $('#content').append(html);
        });                        
    });
    return false;
    });
});

The issue was that the loop was only counting the one row. You needed to get it to count the number of items in the list. That is handled by putting 'data.items' so that it is able to get an accurate count. I'm sorry I didn't catch this earlier. Let me know if this fixes your issue.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your help! But it still doesn't seem to be working: pixeloft.com/clients/popdust
I have edited my answer to give you the solution to the problem after doing a sample myself in the Javascript console for your site.
Not a problem! Glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.