55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
const url = "https://jsonplaceholder.typicode.com/posts";
|
|
|
|
async function fetchAPI() {
|
|
try{
|
|
let result = await fetch(url,
|
|
{
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
|
|
body: JSON.stringify(
|
|
{
|
|
name: "Szabó Márton",
|
|
email: "@mail"
|
|
}
|
|
)
|
|
}
|
|
);
|
|
let data = await result.json();
|
|
console.log(data);
|
|
} catch (error){
|
|
console.error(error);
|
|
} finally {
|
|
console.log("All done.")
|
|
}
|
|
}
|
|
|
|
async function ajax() {
|
|
let xhr = new XMLHttpRequest();
|
|
xhr.open("GET", url, true);
|
|
|
|
xhr.onreadystatechange = async function(){
|
|
if (xhr.readyState === 4){
|
|
if (xhr.status === 200){
|
|
console.log("asd");
|
|
let sol = await JSON.parse(xhr.responseText);
|
|
//let sol = await JSON.parse(JSON.stringify(xhttp.responseText));
|
|
sol.forEach(element => {
|
|
if (element.id > 0 && element.id < 4){
|
|
console.log(element);
|
|
}
|
|
});
|
|
} else if(xhr.status == 400){
|
|
console.error("Az erőforrás nem található");
|
|
} else {
|
|
console.error(`Hiba kód: ${xhr.status}`);
|
|
}
|
|
}
|
|
}
|
|
xhr.send();
|
|
}
|
|
|
|
//fetchAPI();
|
|
ajax(); |