Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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);
    });
})
//...
Source by attacomsian.com #
 
PREVIOUS NEXT
Tagged: #javascript #fetch #APIjson
ADD COMMENT
Topic
Name
8+9 =