JS
Fetch API: leggere un JSON in 10 righe
Esempio pratico: fetch(), gestione errori e rendering minimo.
Esempio
Usa fetch e controlla res.ok. Poi fai res.json() e aggiorna la pagina.
async function load(){
const res = await fetch('/api/example.json');
if(!res.ok) throw new Error('HTTP ' + res.status);
const data = await res.json();
document.querySelector('#out').textContent = JSON.stringify(data, null, 2);
}
load().catch(console.error);