TuttoGuide.net
Home / Guide JavaScript / Fetch API: leggere un JSON in 10 righe
JS

Fetch API: leggere un JSON in 10 righe

Esempio pratico: fetch(), gestione errori e rendering minimo.

Livello: base Aggiornato: 10/01/2026

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);

Risorse utili

← Torna alla categoria