[英]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.