
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <h1>API Request bacon</h1>
    <button type="button" oblick="loadDoc()">Request bacon</button>
    <p id="demo"></p>
    <script>
      function loadDoc() {
        const xhttp = new XMLHttpRequest();
        xhttp.onload = function () {
          document.getElementById("demo").innerHTML = this.responseText;
        };
        xhttp.open("GET", "https://baconipsum.com/api/?type-all-meat");
        xhttp.send();
      }
    </script>
  </body>
</html>
kokkuvõte:
xhttp.open(“GET”, url) – avab uus HTTP päring
xttp.send() – saadab päringu servisse
XMLHttpRequest – AJAX päring mis saadab API aadressile päringu