Kingston_Pendrive/Suli/13.b/Frontend/Javascript/2024. 11. 15/js/script.js

21 lines
651 B
JavaScript
Raw Normal View History

2024-11-19 18:04:02 +00:00
let xhttp = new XMLHttpRequest()
xhttp.open("GET", "https://jsonplaceholder.typicode.com/posts", true)
xhttp.onreadystatechange = function() {
if (xhttp.readyState === 4) {
if (xhttp.status === 200) {
console.log("Sikeres kérés:")
let response = JSON.parse(xhttp.responseText)
for (let i = 0; i < 3; i++) {
console.log(response[i]);
}
} else if (xhttp.status === 404) {
console.log("Erőforrás nem található - 404-es hibakód.")
} else {
console.log(`Hiba történt: ${xhttp.status}`)
}
}
}
xhttp.send()