29 lines
973 B
JavaScript
29 lines
973 B
JavaScript
|
const url = "https://jsonplaceholder.typicode.com/posts";
|
||
|
async function main() {
|
||
|
let xhttp = new XMLHttpRequest();
|
||
|
xhttp.open("GET", url, true);
|
||
|
|
||
|
xhttp.onreadystatechange = async function(){
|
||
|
if (xhttp.readyState === 4){
|
||
|
if (xhttp.status === 200){
|
||
|
let sol = await JSON.parse(xhttp.responseText);
|
||
|
//let sol = await JSON.parse(JSON.stringify(xhttp.responseText));
|
||
|
sol.forEach(element => {
|
||
|
if (element.id > 0 && element.id < 4){
|
||
|
console.log(element);
|
||
|
}
|
||
|
});
|
||
|
//console.log(`Sikeres kérés: ${sol[0].userId}`);
|
||
|
} else if(xhttp.status === 404){
|
||
|
console.log("Az erőforrás nem található - 404 hibakód");
|
||
|
} else{
|
||
|
console.error(`Hiba történt, státuszkód: ${xhttp.status}`);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
xhttp.send();
|
||
|
}
|
||
|
|
||
|
main();
|
||
|
|