Get JSON data from an URL

AJAX request using ES6 Promises

by jarosciak

HTML

<div id="tu-ukaz-json"></div>

JavaScript

let $div1_ = document.querySelector('#tu-ukaz-json');

fetch('https://ipapi.co/8.8.8.8/json/')
  .then(response => {
    if (response.ok) {
      response.json().then(data => {
        $div1_.textContent = JSON.stringify(data);
      });
    } else $div1_.textContent = 'Network response was not ok.';
  })
  .catch(error =>
    $div1_.textContent = 'Fetch error: ' + error
  );