--- id: 5ccfad82bb2dc6c965a848e5 title: Otterenere JSON col metodo fetch di JavaScript challengeType: 6 forumTopicId: 301501 dashedName: get-json-with-the-javascript-fetch-method --- # --description-- Un altro metodo per richiedere dati esterni è usare il metodo JavaScript `fetch()`. È equivalente a `XMLHttpRequest`, ma la sintassi è considerata più facile da capire. Ecco il codice per fare una richiesta GET a `/json/cats.json` ```js fetch('/json/cats.json') .then(response => response.json()) .then(data => { document.getElementById('message').innerHTML = JSON.stringify(data); }) ``` Dai un'occhiata ad ogni parte di questo codice. La prima linea è quella che fa la richiesta. Così, `fetch(URL)` fa una richiesta `GET` all'URL specificato. Il metodo restituisce una Promise. Dopo che una Promise è restituita, se la richiesta ha avuto successo, il metodo `then` viene eseguito, ed esso converte la risposta in un formato JSON. Anche il metodo `then` restituisce una Promise, gestita dal metodo `then` successivo. L'argomento nel secondo metodo `then` è l'oggetto JSON che stai cercando! Ora, l'elemento che riceverà i dati è selezionato usando `document.getElementById()`. Quindi il codice HTML dell'elemento è modificato inserendo una stringa creata dall'oggetto JSON restituito dalla richiesta. # --instructions-- Modifica il codice per creare e usare una richiesta `GET` all'API Cat Photo di freeCodeCamp. Ma questa volta, usando il metodo `fetch` invece di `XMLHttpRequest`. # --hints-- Il tuo codice dobrebbe fare una richiesta `GET` usando `fetch`. ```js assert(code.match(/fetch\s*\(\s*('|")\/json\/cats\.json\1\s*\)/g)); ``` Il tuo codice dovrebbe usare `then` per convertire la risposta in JSON. ```js assert( code.match( /\.then\s*\(\s*\(?(?\w+)\)?\s*=>\s*\k\s*\.json\s*\(\s*\)\s*\)/g ) ); ``` Il tuo codice dovrebbe usare `then` per gestire i dati convertiti a JSON dall'altro `then`. ```js assert(__helpers.removeWhiteSpace(code).match(/\.then\(\(?\w+\)?=>{[^}]*}\)/g)); ``` Il tuo codice dovrebbe selezionare l'elemento con id `message` e cambiare il suo innerHTML con la stringa di dati JSON. ```js assert( __helpers.removeWhiteSpace(code).match( /document\.getElementById\(('|")message\1\)\.innerHTML=JSON\.stringify\(?\w+\)/g ) ); ``` # --seed-- ## --seed-contents-- ```html

Cat Photo Finder

The message will go here

``` # --solutions-- ```html

Cat Photo Finder

The message will go here

```