Javascript fetch API如何通过在函数中传递JSON对象将书籍标题呈现到DOM中
我有一个实验室,需要使用fetch()获取数据Javascript fetch API如何通过在函数中传递JSON对象将书籍标题呈现到DOM中,javascript,api,fetch,Javascript,Api,Fetch,我有一个实验室,需要使用fetch()获取数据 // To pass the tests, don't forget to return your fetch! fetch('https://anapioficeandfire.com/api/books') .then(function(response) { return response.json(); }) .then(function(titles) { return titles }); renderBooks(titles)
// To pass the tests, don't forget to return your fetch!
fetch('https://anapioficeandfire.com/api/books')
.then(function(response) {
return response.json();
})
.then(function(titles) {
return titles
});
renderBooks(titles)
}
function renderBooks() {
const main = document.querySelector('main');
books.forEach(book => {
const h2 = document.createElement('h2');
h2.innerHTML = book.name;
main.appendChild(h2);
});
}
document.addEventListener('DOMContentLoaded', function() {
fetchBooks();
});
我收到的错误是没有定义标题。目标是通过将JSON对象传递给renderBooks(),将书籍标题呈现到DOM中。这是否回答了您的问题?这个例子有一些地方不对劲
renderBooks
不接受参数<代码>渲染书(标题)在然后之外<代码>获取书籍
未定义。