Készen van az AJAX verziója a POST kérésnek (keddi Frontend óra)

This commit is contained in:
Tóth Ádám 2024-11-20 17:16:40 +01:00
parent 11dcfcba45
commit 2f5d250d5a
2 changed files with 35 additions and 6 deletions

View File

@ -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)
@ -27,3 +28,27 @@ 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)
})

View File

@ -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) => {
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,6 +32,7 @@ app.post("/feltoltes", (req, res) => {
}
})
res.status(201).send(JSON.stringify({data: adat}))
})
})