JavaScript Fetch Apijson
//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);
});
})
//...
JB Will