2013-03-06 74 views
0

我试图显示来自Google API的数据,但不断收到“undefined”作为结果。这里是我的代码:Google API Json Ajax/jquery调用

$(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; 
    }); 
}); 

回答

2

您的代码应该如下所示让你的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; 
    }); 
}); 

问题是,环仅数着一排。您需要让它计算列表中的项目数量。这是通过放置'data.items'来处理的,以便能够获得准确的计数。对不起,我没有早点收到。让我知道这是否解决了您的问题。

+0

谢谢你的帮助!但它似乎仍然没有工作:http://pixeloft.com/clients/popdust/ – user2140126 2013-03-07 04:05:56

+0

我已经编辑了我的答案,在为您的网站的Javascript控制台做了一个自己的示例后,给你解决问题的方法。 – 2013-03-07 12:25:10

+0

This Works!非常感谢! – user2140126 2013-03-07 14:09:49