Ricordati che le API (o Application Programming Interface) sono strumenti che i computer usano per comunicare tra di loro. Imparerai come aggiornare l'HTML con i dati che otteniamo dalle API usando una tecnologia chiamata AJAX.
La sintassi JSON è molto simile alla notazione letterale degli oggetti in JavaScript. JSON ha le proprietà degli oggetti e i loro valori correnti tra un `{` e un `}`.
Tuttavia, JSON viene inviato dalle API in forma di `bytes`, e la tua applicazione lo riceve come `string`. Questi possono essere convertiti in oggetti JavaScript, ma non sono oggetti JavaScript di default. Il metodo `JSON.parse` analizza la stringa e costruisce l'oggetto JavaScript da essa descritto.
Ecco un ripasso di quello che sta facendo ognuna delle parti in gioco. L'oggetto JavaScript `XMLHttpRequest` ha una serie di proprietà e metodi che vengono utilizzati per trasferire dati. In primo luogo, un'istanza dell'oggetto `XMLHttpRequest` viene creata e salvata nella variabile `req`. Successivamente, il metodo `open` inizializza una richiesta: questo esempio richiede dati da un'API, quindi è una richiesta `GET`. Il secondo argomento per `open` è l'URL dell'API dalla quale stai richiedendo i dati. Il terzo argomento è un valore booleano dove `true` la rende una richiesta asincrona. Il metodo `send` invia la richiesta. Infine, il gestore di evento `onload` analizza i dati restituiti e applica il metodo `JSON.stringify` per convertire l'oggetto JavaScript in una stringa. Questa stringa viene quindi inserita come testo del messaggio.
Modifica il codice per creare e usare una richiesta `GET` all'API Cat Photo di freeCodeCamp. Quindi fai click sul pulsante `Get Message`. La tua funzione AJAX sostituirà il testo `The message will go here` con l'output JSON grezzo ricevuto dall'API.