首页 > 解决方案 > 谷歌图书 API

问题描述

我想遍历 google 图书 API 提供的 items 数组并在 div 中打印结果,但不知何故我无法这样做。这是我写到现在的。

<body>
    <div class="main-body">
        <form id="form">
        <div class="form-group">
            <label for="usr">Enter Book Name</label>
            <input type="search" class="form-control" id="search-text">
        </div>
        <div class="search-button">
            <button onclick="function2();" type="button" id="search-button" class="btn btn-default">Search</button>
        </div>
        </form>
        <div id="result">
            <!--something seems wrong here-->
        <h3 class="title"></h3>
        <h4 class="author"></h4>

        <img src="" alt="" class="thumbnail">
        </div>

    </div>
    <script>
     function function2(){
         var result = document.getElementById('search-text').value;
         $.ajax({
            url: "https://www.googleapis.com/books/v1/volumes?q="+result,
            type: 'GET',
            dataType: 'json', // added data type
            success: handleResponse
        });
        function handleResponse(res){
            $.each(res.items,function(i,item){
                var title = item.volumeInfo.title,
                    author = item.volumeInfo.authors[0],
                    thumb = item.volumeInfo.imageLinks.thumbnail;
            <!--want to iterate over each element in items array and print it-->
            $('.title').text(title);
            $('.author').text(author);
            $('.thumbnail').attr('src',thumb);
        })
        } 
       }
    </script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</body>

标签: javascripthtmlapigoogle-api

解决方案


您当前的代码在每次迭代中将先前的数据替换为当前数据。做你想做的最简单的方法应该是构建新元素并将它们附加到你的“结果”div,如下所示。

我还建议验证数据。我用没有封面或作者的退回书籍测试了一些查询。

function function2() {
  var result = document.getElementById('search-text').value;
  $.ajax({
    url: "https://www.googleapis.com/books/v1/volumes?q=" + result,
    type: 'GET',
    dataType: 'json', // added data type
    success: handleResponse
  });

  function handleResponse(res) {
    $.each(res.items, function(i, item) {
        var title = item.volumeInfo.title,
          author = item.volumeInfo.authors[0],
          thumb = item.volumeInfo.imageLinks.thumbnail;
        var elTitle = $('<h3 class="title"></h3>').html(title),
          elAuthor = $('<h4 class="author"></h4>').html(author),
          elThumb = $('<img src="" alt="" class="thumbnail">').attr('src', thumb);
        $('#result').append(elTitle, elAuthor, elThumb);
    })
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<body>
  <div class="main-body">
    <form id="form">
      <div class="form-group">
        <label for="usr">Enter Book Name</label>
        <input type="search" class="form-control" id="search-text">
      </div>
      <div class="search-button">
        <button onclick="function2();" type="button" id="search-button" class="btn btn-default">Search</button>
      </div>
    </form>
    <div id="result">
    </div>
  </div>
</body>


推荐阅读