fetch('./yourjson.json')
.then((response) => response.json())
.then((data) => {
console.log(data);
})
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
// your expected POST request payload goes here
title: "My post title",
body: "My post content."
})
})
.then(res => res.json())
.then(data => {
// enter you logic when the fetch is successful
console.log(data)
})
.catch(error => {
// enter your logic for when there is an error (ex. error toast)
console.log(error)
})
fetch("localhost:3000/api/home") // first step
.then(response => response.json()) // second step
.then(data => {
console.log(data)
})
.catch(error => console.error(error))
var myRequest = new Request('products.json');//GET
var myRequest = new Request('products.json', {method: "post"});//POST
fetch(myRequest)
.then(response => response.json())
.then(data => {
console.log(data);
})
.catch(console.error);
fetchJsonp('/users.jsonp')
.then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json', json)
}).catch(function(ex) {
console.log('parsing failed', ex)
})
fetch("animals.json")
.then(response => response.json())
.then(data => {
// Work with your data here
});