繁体   English   中英

我没有获得每页特定数量的获取图片,而是获得随机数量的项目

[英]Instead of getting a specific number of fetched pictures per page, I am getting a random number of items

function getResults(querySearch) {
  refs.list.innerHTML = '';
  if (querySearch.length > 0) {
    fetch(
      `https://pixabay.com/api/?image_type=photo&orientation=horizontal&q=${querySearch}&page=${api.pageNum}&per_page=12&key=${api.key}`,
    )
      .then(data => {
        return data.json();
      })
      .then(pic => {
        const info = pic.hits.map(item => template(item)).join('');
        refs.list.insertAdjacentHTML('beforeend', info);
      })
      .catch(err => {
        throw err;
      });
  }
}

我没有获得每页特定数量的获取图片,而是获得随机数量的项目。 我不知道为什么会发生这种情况,因为应该获取的项目数量在 url (12) 中是硬编码的。 发生了什么,我应该改变什么?

API 工作正常。 我认为问题在于您的功能,例如模板。

您需要更具体地了解您使用的问题和功能。

但是这段代码对我有用:

 fetch( `https://pixabay.com/api/?image_type=photo&orientation=horizontal&q=flower&page=2&per_page=12&key=api_key` ).then((res) => res.json()).then((pics) => { pics.hits.forEach((pic) => { var img = document.createElement("img"); var wrapper = document.querySelector(".wrapper"); img.src = pic.largeImageURL; img.alt = pic.tags; img.width = 200; wrapper.append(img); }); }).catch((e) => { console.log(e); });
 <div class="wrapper"></div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM