added Kutya
This commit is contained in:
55
ajax_fetch.js
Normal file
55
ajax_fetch.js
Normal file
@@ -0,0 +1,55 @@
|
||||
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();
|
||||
Reference in New Issue
Block a user