21 lines
651 B
JavaScript
21 lines
651 B
JavaScript
|
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()
|