how to use fetch() javascript
JavaScript
//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 website
fetch('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 ...
function fetchBooks() {
return fetch('https://anapioficeandfire.com/api/books')
.then(resp => resp.json())
.then(json => renderBooks(json));
}
function renderBooks(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()
})
fetch('http://example.com/movies.json')
.then((response) => {
return response.json();
})
.then((data) => {
console.log(data);
});
fetch('http://example.com/movies.json')
.then((response) => {
return response.json();
})
.then((myJson) => {
console.log(myJson);
});developer.mozilla.org
Also in JavaScript:
- Title
- how to deploy react app in tomcat server
- Category
- JavaScript
- Title
- how to remove the id in mongoose schema
- Category
- JavaScript
- Title
- express js list all routes
- Category
- JavaScript
- Title
- equal to or more than javascript
- Category
- JavaScript
- Title
- iterate over object javascript
- Category
- JavaScript
- Title
- clear the console while in strict mode
- Category
- JavaScript
- Title
- get current directory vbscript
- Category
- JavaScript
- Title
- How to check whether a string contains a substring in JavaScript?
- Category
- JavaScript
- Title
- how to validate input type=date in javascript
- Category
- JavaScript
- Title
- express bodyparser deprecated
- Category
- JavaScript
- Title
- get utc time javascript
- Category
- JavaScript
- Title
- command to start api on react
- Category
- JavaScript
- Title
- angular ngclass
- Category
- JavaScript
- Title
- chrome extension get current tab from popup
- Category
- JavaScript
- Title
- disable autocomplete javascript
- Category
- JavaScript
- Title
- comment obtenir l'élément parent javascript
- Category
- JavaScript
- Title
- array matching
- Category
- JavaScript
- Title
- javascript alert
- Category
- JavaScript
- Title
- ajax open a request
- Category
- JavaScript
- Title
- interaction js drag anddrop
- Category
- JavaScript
- Title
- getcollectionnames
- Category
- JavaScript
- Title
- count number of times an element is occuring in an array in javascript
- Category
- JavaScript
- Title
- how to javascript
- Category
- JavaScript
- Title
- filter out arrays js
- Category
- JavaScript
- Title
- javascript array exists check
- Category
- JavaScript
- Title
- enzyme check state
- Category
- JavaScript
- Title
- Javascript case insensitive string comparison
- Category
- JavaScript
- Title
- how to get textedit on mac without download
- Category
- JavaScript
- Title
- execute js with innerhtml
- Category
- JavaScript
- Title
- function prototype javascript
- Category
- JavaScript
- Title
- javascript Check if an element is a descendant of another
- Category
- JavaScript
- Title
- faker js
- Category
- JavaScript
- Title
- how to make an async function
- Category
- JavaScript
- Title
- beautify json python
- Category
- JavaScript
- Title
- add char in specific index stirng javascript
- Category
- JavaScript
- Title
- check palindrome js
- Category
- JavaScript
- Title
- how to move a block in javascript
- Category
- JavaScript
- Title
- angular 6 reload current page
- Category
- JavaScript
- Title
- How do I check whether a checkbox is checked in jQuery?
- Category
- JavaScript
- Title
- input set variable angular
- Category
- JavaScript
- Title
- function js
- Category
- JavaScript
- Title
- best react native animation library
- Category
- JavaScript
- Title
- how to add button in alert box in react native
- Category
- JavaScript
- Title
- javascript append to object
- Category
- JavaScript
- Title
- get the id of a div in jquery
- Category
- JavaScript
- Title
- add countdown timer to javascript quiz
- Category
- JavaScript
- Title
- angular attach component to body
- Category
- JavaScript
- Title
- fivem trigger javascript function
- Category
- JavaScript
- Title
- array.filter in javascript not working in w3schools
- Category
- JavaScript
- Title
- declare function javascript
- Category
- JavaScript
- Title
- import react dom
- Category
- JavaScript
- Title
- expo open app settings
- Category
- JavaScript
- Title
- create react native app npx
- Category
- JavaScript
- Title
- how to turn decimales into percents with javascript
- Category
- JavaScript
- Title
- comment out in javascript
- Category
- JavaScript
- Title
- express js redirect to url
- Category
- JavaScript
- Title
- If 'ion-item' is an Angular component, then verify that it is part of this module.
- Category
- JavaScript
- Title
- angular countdown timer
- Category
- JavaScript
- Title
- class javascript
- Category
- JavaScript
- Title
- change the focus to next in angular forms
- Category
- JavaScript
- Title
- how to check if variable is array in javascript
- Category
- JavaScript
- Title
- href before onclick js
- Category
- JavaScript
- Title
- how to add border to a text in html with javascript
- Category
- JavaScript
- Title
- get the state of a checkbox
- Category
- JavaScript
- Title
- deny ready jquery
- Category
- JavaScript
- Title
- how to untick a checkbox with javascript
- Category
- JavaScript
- Title
- angular 8 to 9
- Category
- JavaScript
- Title
- how to get the height of window in javascript
- Category
- JavaScript
- Title
- how to remove a property from an object in javascript
- Category
- JavaScript
- Title
- deploy angular app on google app engine
- Category
- JavaScript
- Title
- check if object has method javascript
- Category
- JavaScript
- Title
- get url react
- Category
- JavaScript
- Title
- enzyme test method
- Category
- JavaScript
- Title
- add and remove class in jquery
- Category
- JavaScript
- Title
- gql TypeError: Object(...) is not a function
- Category
- JavaScript
- Title
- convert json to 2d array
- Category
- JavaScript
- Title
- chrome add a bookmark that appends to current url
- Category
- JavaScript
- Title
- angular ng-click toggle class
- Category
- JavaScript
- Title
- if statemnt shorthand js without else
- Category
- JavaScript
- Title
- array map javascript
- Category
- JavaScript
- Title
- if statement in ejs
- Category
- JavaScript
- Title
- javascript add function to onchange event
- Category
- JavaScript
- Title
- convert hashmap into json
- Category
- JavaScript
- Title
- angular wait all subscriptions
- Category
- JavaScript
- Title
- client side redirect javascript
- Category
- JavaScript
- Title
- javascript capitalize first letter
- Category
- JavaScript
- Title
- image popup js close button
- Category
- JavaScript
- Title
- how to check string uppercase or lowersace using regex javascript
- Category
- JavaScript
- Title
- clear the command prompt node
- Category
- JavaScript
- Title
- duplicate encoder
- Category
- JavaScript
- Title
- how to add onclick event in javascript
- Category
- JavaScript
- Title
- for loop javascript
- Category
- JavaScript
- Title
- how to disable cash through get request in angular
- Category
- JavaScript
- Title
- how to change a string to number in javascript
- Category
- JavaScript
- Title
- javascript base64 encode string
- Category
- JavaScript
- Title
- .pop js
- Category
- JavaScript
- Title
- how to add role in discord.js v12
- Category
- JavaScript
- Title
- how to add changes every time you route navigate to page in angular
- Category
- JavaScript
- Title
- async await javascript push
- Category
- JavaScript
- Title
- find particular object from array in js
- Category
- JavaScript