1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| function getUserList() { let newListbox = document.createElement('li'); newListbox.innerHTML = `<li class="list-group-item head">User List</li>`;
const url = '/users'; const fetchResponsePromise = fetch(url); fetchResponsePromise .then((response) => { if (response.status === 200) { return response.json(); } else { throw new Error('Something went wrong on api server!'); } }) .then((response) => { console.log(`fetch`, response); let i = 0; for (const key in response) { i++; if (response.hasOwnProperty(key)) { const element = response[key];
const new_li_element = document.createElement('li'); new_li_element.className = 'list-group-item'; new_li_element.appendChild(document.createTextNode(element)); newListbox.appendChild(new_li_element); } } listbox.innerHTML = newListbox.innerHTML; }) .catch((error) => { console.error(error); }); }
|