Ava veebilehitsejas Code Sandbox sait
Vali Official Templates alt static

Kirjuta pildil olev kood index.html faili. Alustuseks kasuta HTML trafaretti (hüüumärk ja tab klahv).
<!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>API Päringud</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<script>
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function () {
document.getElementById("demoBacon").innerHTML = this.responseText;
};
xhttp.open("GET", "https://baconipsum.com/api/?type=all-meat");
xhttp.send();
}
function loadJoke() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function () {
const response = JSON.parse(this.responseText);
document.getElementById("demoJoke").innerHTML = response.joke;
};
xhttp.open("GET", "https://v2.jokeapi.dev/joke/Any?type=single", true);
xhttp.send();
}
function loadFact() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function () {
const response = JSON.parse(this.responseText);
document.getElementById("demoFact").innerHTML = response.text;
};
xhttp.open(
"GET",
"https://uselessfacts.jsph.pl/api/v2/facts/random",
true
);
xhttp.send();
}
</script>
<h2>API Päringud</h2>
<button type="button" onclick="loadDoc()">Request Bacon</button>
<p id="demoBacon"></p>
<button type="button" onclick="loadJoke()">Request Joke</button>
<p id="demoJoke"></p>
<button type="button" onclick="loadFact()">Request Fact</button>
<p id="demoFact"></p>
</body>
</html>
Stiilid:
body {
font-family: Arial, sans-serif;
background: #fff;
color: #000;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
min-height: 100vh;
margin: 0;
padding: 20px;
text-align: center;
}
h2 {
margin-bottom: 20px;
}
button {
background: #000;
color: #fff;
border: none;
border-radius: 20px;
padding: 10px 20px;
margin: 10px;
cursor: pointer;
font-size: 14px;
transition: background 0.3s;
}
button:hover {
background: #333;
}
p {
max-width: 600px;
margin: 10px 0 20px;
}
Tulemus:
