-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (74 loc) · 2.04 KB
/
script.js
File metadata and controls
82 lines (74 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
fetch("http://localhost:8083/students")
.then(response => response.json())
.then(students => {
console.log("Students from backend:", students);
const list = document.getElementById("studentList");
list.innerHTML = "";
students.forEach(s => {
const li = document.createElement("li");
li.textContent = s.name + " | " + s.email;
list.appendChild(li);
});
})
.catch(error => {
console.error("Error:", error);
});
function addStudent() {
const name = document.getElementById("name").value;
const email = document.getElementById("email").value;
const password = document.getElementById("password").value;
fetch("http://localhost:8083/students", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
name: name,
email: email,
password: password
})
})
.then(response => response.json())
.then(data => {
alert("Student Added Successfully");
// clear form
document.getElementById("name").value = "";
document.getElementById("email").value = "";
document.getElementById("password").value = "";
// reload list
location.reload();
})
.catch(error => {
console.error("Error:", error);
});
}
function loginStudent() {
const email = document.getElementById("loginEmail").value;
const password = document.getElementById("loginPassword").value;
const msg = document.getElementById("loginMsg");
fetch("http://localhost:8083/students/login", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
email: email,
password: password
})
})
.then(res => res.text()) // 🔥 IMPORTANT CHANGE
.then(data => {
if (data.startsWith("Login Successful")) {
msg.style.color = "green";
msg.innerText = data;
} else {
msg.style.color = "red";
msg.innerText = "Invalid email or password";
}
})
.catch(err => {
msg.style.color = "red";
msg.innerText = "Server error";
console.error(err);
});
}