added ajax
This commit is contained in:
parent
262a940747
commit
01b1a717df
BIN
24_11_15/24_11_15 - Frontend.pptx
Normal file
BIN
24_11_15/24_11_15 - Frontend.pptx
Normal file
Binary file not shown.
31
24_11_15/ajaxpost.js
Normal file
31
24_11_15/ajaxpost.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
const url = "https://jsonplaceholder.typicode.com/posts";
|
||||
|
||||
|
||||
async function main() {
|
||||
let adat = {
|
||||
title: "Új feladat",
|
||||
complted: false
|
||||
};
|
||||
|
||||
let xhttp = new XMLHttpRequest();
|
||||
xhttp.open("POST", url, true);
|
||||
xhttp.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
|
||||
|
||||
xhttp.onreadystatechange = async function(){
|
||||
if (xhttp.readyState === 4){
|
||||
//201 POST esetén sikeres adatfeltöltés statusa
|
||||
if (xhttp.status === 201){
|
||||
let sol = await JSON.parse(xhttp.responseText);
|
||||
console.log(`Sikeres adatfeltöltés: ${sol}`);
|
||||
} 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(JSON.stringify(adat));
|
||||
}
|
||||
|
||||
main();
|
||||
|
11
24_11_15/index.html
Normal file
11
24_11_15/index.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Document</title>
|
||||
</head>
|
||||
<body>
|
||||
<script src="ajaxpost.js" defer></script>
|
||||
</body>
|
||||
</html>
|
28
24_11_15/script.js
Normal file
28
24_11_15/script.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
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();
|
||||
|
Loading…
Reference in New Issue
Block a user