//Most API's will only allow you to fetch on their website.
//This means you couldn't run this code in the console on
// google.com because:// 1. Google demands the fetch request be from https// 2. open-notify's API blocks the request outside of their websitefetch('http://api.open-notify.org/astros.json')
.then(function(response) {
return response.json();
})
.then(function(json) {
console.log(json)
});
// Here is another example. A method (function) that // grabs Game of Thrones books from an API ...functionfetchBooks() {
returnfetch('https://anapioficeandfire.com/api/books')
.then(resp => resp.json())
.then(json =>renderBooks(json));
}
functionrenderBooks(json) {
const main = document.querySelector('main')
json.forEach(book => {
const h2 = document.createElement('h2')
h2.innerHTML = `<h2>${book.name}</h2>`
main.appendChild(h2)
})
}
document.addEventListener('DOMContentLoaded', function() {
fetchBooks()
})
Posted by: Guest
on March-30-2020
Code answers related to "how to return fetch response js"
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems
resetting your password contact us
Check Your Email and Click on the link sent to your email