fetch('./yourjson.json')
.then((response) => response.json())
.then((data) => {
console.log(data);
})
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);
//Set URL from External Variable
const url = '{{url}}';
//Set Admin P from Input variable
const adminP = '{{adminP}}';
//Set Admin Password from input variable
const adminPW = '{{adminPW}}';
fetch(url)
.then(res => res.json())
.then(data => {
// code to handle the response
}).catch(err => {
console.error('Error: ', err);
});
// create an element
const createNode = (elem) => {
return document.createElement(elem);
};
// append an element to parent
const appendNode = (parent, elem) => {
parent.appendChild(elem);
}
// ...
.then(data => {
// iterate over response
data.map((responseD) => {
// create the elements
let li = createNode('li'),
img = createNode('img'),
span = createNode('span');
img.src = user.avatar_url;
span.innerText = user.login;
// append all elements
appendNode(li, img);
appendNode(li, span);
appendNode(ul, li);
});
})
//...
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
});