Frontend/24_10_15/fetch.js
2024-10-15 13:15:08 +02:00

54 lines
1.3 KiB
JavaScript

let url = "https://jsonplaceholder.typicode.com/photos";
document.getElementById("lekeres").addEventListener("click", function () {
fetch(url)
.then(response => response.json())
.then(data => {
for (let index = 0; index < 3; index++) {
let cim = document.createElement("h2");
cim.innerHTML = data[index].title;
let element = document.createElement("img");
element.src = data[index].thumbnailUrl;
let divelement = document.getElementById(`img_${index}`);
divelement.appendChild(cim);
divelement.appendChild(element);
}
})
.catch(data => console.log(data));
});
// post
/*
let path = "response.json";
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: "Példa Péter",
email: "pelda_peter@gmail.com"
}),
}).then(response => response.json()).then(data => console.log(data));
//PUT
fetch(url, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name: "Példa Péter",
email: "pelda_peter@gmail.com"
}),
}).then(response => response.json()).then(data => console.log(data));
*/