javascript - 谷歌图书 API

标签 javascript html api google-api

我想遍历 google books API 提供的项目数组并将结果打印在 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>

最佳答案

您当前的代码会在每次迭代时用当前数据替换以前的数据。 做你想做的最简单的方法应该是构建新元素和append它们添加到您的“结果”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>

关于javascript - 谷歌图书 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53503036/

相关文章:

javascript - 如何使 Bootstrap Tour 应用 GET 参数

javascript - 按 'ctrl' 键时,jQuery 选项卡插件未在新浏览器选项卡中打开

javascript - Angular&kendo ui,宽度和高度kendo窗口

javascript - 通过单击多选框中的项目添加到 HTML 文本字段

javascript - 使用 Axios 和 React 通过 API 进行过滤

node.js - 为现有 NodeJS 服务器生成 Swagger 文档

javascript - 错误: Uncaught TypeError: Cannot read property 'offsetWidth' of null in angularjs for google maps

javascript - 在 $.each 循环中,获取两种不同类型的对象返回值和未定义

html - 有没有办法强制 HTML <ul> 为固定大小以制作 SPA?

c# - 架构问题 : use of dependency injection resulting in rubbish API