diff --git a/Suli/13.b/Frontend/Javascript/2024. 11. 18/public/js/scriprAJAX.js b/Suli/13.b/Frontend/Javascript/2024. 11. 18/public/js/scriptAJAX.js similarity index 53% rename from Suli/13.b/Frontend/Javascript/2024. 11. 18/public/js/scriprAJAX.js rename to Suli/13.b/Frontend/Javascript/2024. 11. 18/public/js/scriptAJAX.js index d231a9e..86ad538 100644 --- a/Suli/13.b/Frontend/Javascript/2024. 11. 18/public/js/scriprAJAX.js +++ b/Suli/13.b/Frontend/Javascript/2024. 11. 18/public/js/scriptAJAX.js @@ -1,6 +1,7 @@ -const button = document.querySelector("button") +const lekerdezes = document.getElementById("lekerdezes") +const bekuldes = document.getElementById("bekuldes") -button.addEventListener("click", () => { +lekerdezes.addEventListener("click", () => { let xhttp = new XMLHttpRequest() xhttp.open("GET", "../json/munkavallalok.json", true) @@ -26,4 +27,28 @@ button.addEventListener("click", () => { } xhttp.send() +}) + +bekuldes.addEventListener("click", () => { + let xhttp = new XMLHttpRequest() + xhttp.open("POST", "/feltoltes", true) + xhttp.setRequestHeader('Content-type', 'application/json; charset=UTF-8') + + xhttp.onreadystatechange = function() { + if (xhttp.readyState === 4) { + if (xhttp.status === 201) { + console.log(JSON.parse(xhttp.responseText)); + } + } + } + + const munkavallalo = document.querySelector("textarea").value.split("\n") + const munkavallaloJSON = JSON.stringify({ + nev: munkavallalo[0], + beosztas: munkavallalo[1], + kor: munkavallalo[2], + fizetes: munkavallalo[3] + }) + + xhttp.send(munkavallaloJSON) }) \ No newline at end of file diff --git a/Suli/13.b/Frontend/Javascript/2024. 11. 18/server.js b/Suli/13.b/Frontend/Javascript/2024. 11. 18/server.js index 6a23ebc..f9ae403 100644 --- a/Suli/13.b/Frontend/Javascript/2024. 11. 18/server.js +++ b/Suli/13.b/Frontend/Javascript/2024. 11. 18/server.js @@ -15,11 +15,14 @@ app.get("/", (req, res) => { }) app.post("/feltoltes", (req, res) => { - fs.readFile("F:\\Suli\\13.b\\Frontend\\Javascript\\2024. 11. 18\\public\\json/munkavallalok.json", "utf8", (err, data) => { - let adat = JSON.parse(data) + const pendrive = "F:\\Suli\\13.b\\Frontend\\Javascript\\2024. 11. 18\\public\\json\\munkavallalok.json" + const gep = "D:\\Kingston_Pendrive\\Suli\\13.b\\Frontend\\Javascript\\2024. 11. 18\\public\\json\\munkavallalok.json" + + fs.readFile(gep, "utf8", (err, data) => { + let adat = JSON.parse(data) adat.push(req.body) - fs.writeFile("F:\\Suli\\13.b\\Frontend\\Javascript\\2024. 11. 18\\public\\json/munkavallalok.json", JSON.stringify(adat), (err, data) => { + fs.writeFile(gep, JSON.stringify(adat), (err, data) => { if (err) { console.log(err); return @@ -29,7 +32,8 @@ app.post("/feltoltes", (req, res) => { } }) + res.status(201).send(JSON.stringify({data: adat})) }) }) -app.listen(3000) +app.listen(3000) \ No newline at end of file