added ajax
This commit is contained in:
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();
|
||||
|
||||
Reference in New Issue
Block a user