added ajax and fetch CORS error example, this code should not work unless you run it in xammp
This commit is contained in:
57
24_11_18/feladat1.js
Normal file
57
24_11_18/feladat1.js
Normal file
@@ -0,0 +1,57 @@
|
||||
const url = "http://127.0.0.1/PHP_digivagyok/20241118_JS/munkasok.json";
|
||||
|
||||
document.getElementById("button").addEventListener("click", function(){
|
||||
let value = document.getElementById("selection").value;
|
||||
console.log(value);
|
||||
main(value);
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
/*
|
||||
async function main(munkasNumber) {
|
||||
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);
|
||||
|
||||
console.log(sol);
|
||||
|
||||
createDomElement(sol[munkasNumber - 1]);
|
||||
} 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();
|
||||
}
|
||||
*/
|
||||
|
||||
async function main(munkasNumber) {
|
||||
try{
|
||||
let response = await fetch(url);
|
||||
|
||||
if (!response.ok){
|
||||
throw new Error(`Hiba történt: ${response.status}`);
|
||||
}
|
||||
|
||||
let adatok = await response.json();
|
||||
|
||||
createDomElement(adatok[munkasNumber - 1]);
|
||||
} catch (error) {
|
||||
console.error(`Hiba ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function createDomElement(element) {
|
||||
let line = document.createElement("div");
|
||||
line.innerHTML = `Név: ${element.nev}, Beosztás: ${element.beosztas}, Kor: ${element.kor}, Fizetés:${element.fizetes}Ft`;
|
||||
document.getElementById("eredmeny").appendChild(line);
|
||||
}
|
||||
Reference in New Issue
Block a user