See mozilla
fetch('http://example.com/movies.json')
.then((response) => response.json())
.then((data) => console.log(data));
const q = (x,y=document) => y.querySelector(x)
const qq = (x,y=document) => Array.from(y.querySelectorAll(x))
const reload_content = _ => {
fetch("/update")
.then(response => response.text())
.then(text=> {
q("div.content").innerHTML = text
})
}
async await
Fetch a list form a server and display it
let idx = 5
async function update() {
mpdn.textContent = `${idx}`
mpddiv.textContent = "Fetching..." // will show while fetch is pending
let response = await fetch(`backend.php?${idx}`)
let data = await response.json()
let ul = document.createElement("ul")
data.forEach(x => {
const li = document.createElement("li")
li.textContent = x
ul.append(li)
})
mpddiv.textContent = ""
mpddiv.append(ul)
}