Retrieving JSON via AJAX in JavaScript

A common practice is returning data in the JSON format by a server. Let a server have the following file:

{ "num1": "1", "num2": "2" }

Let's obtain data from this file:

button.addEventListener('click', function() { fetch('/data.json').then(response => { return response.text(); }).then(data => { console.log(JSON.parse(data)); }); });

You can use the json method to automatically convert JSON to a JavaScript structure:

button.addEventListener('click', function() { fetch('/data.json').then(response => { return response.json(); }).then(data => { console.log(data); }); });

Let a server return an array in JSON. Loop through this array and write each element in a separate li.

enru