-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_ajax.html
132 lines (109 loc) · 4.51 KB
/
my_ajax.html
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AJAX CRUD Page</title>
</head>
<body>
<h1>AJAX CRUD Page</h1>
ID: <input type="text" id="student_id" /><br><br>
NAME: <input type="text" id="student_name" /><br><br>
AGE: <input type="number" id="student_age" /><br><br>
MAJOR: <input type="text" id="student_major" /><br><br>
<button onclick="getData()">Get Data</button><br><br>
<button onclick="createData()">Create Data</button><br><br>
<button onclick="updateData()">Update Data</button><br><br>
<button onclick="deleteData()">Delete Data</button><br><br>
<div id="student_list"></div>
<script>
function getData() {
const xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:3000/students");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send();
xhr.onload = () => {
if (xhr.status === 200) {
const students = JSON.parse(xhr.responseText);
displayStudents(students);
} else {
console.log("Error:", xhr.status, xhr.statusText);
}
};
}
function createData() {
const name = document.getElementById("student_name").value;
const age = document.getElementById("student_age").value;
const major = document.getElementById("student_major").value;
if (!name || !age || !major) {
alert("Please enter name, age, and major.");
return;
}
const xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:3000/students");
xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
const data = JSON.stringify({ name, age, major });
xhr.send(data);
xhr.onload = () => {
if (xhr.status === 201) {
alert("Data created successfully!");
getData();
} else {
console.log("Error:", xhr.status, xhr.statusText);
}
};
}
function updateData() {
const id = document.getElementById("student_id").value;
const name = document.getElementById("student_name").value;
const age = document.getElementById("student_age").value;
const major = document.getElementById("student_major").value;
if (!id || !name || !age || !major) {
alert("Please enter ID, name, age, and major.");
return;
}
const xhr = new XMLHttpRequest();
xhr.open("PUT", `${"http://localhost:3000/students"}/${id}`);
xhr.setRequestHeader("Content-Type", "application/json; charset=UTF-8");
const data = JSON.stringify({ name, age, major });
xhr.send(data);
xhr.onload = () => {
if (xhr.status === 200) {
alert("Data updated successfully!");
getData();
} else {
console.log("Error:", xhr.status, xhr.statusText);
}
};
}
function deleteData() {
const id = document.getElementById("student_id").value;
if (!id) {
alert("Please enter ID.");
return;
}
const xhr = new XMLHttpRequest();
xhr.open("DELETE", `${"http://localhost:3000/students"}/${id}`);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send();
xhr.onload = () => {
if (xhr.status === 200) {
alert("Data deleted successfully!");
getData();
} else {
console.log("Error:", xhr.status, xhr.statusText);
}
};
}
function displayStudents(students) {
const studentList = document.getElementById("student_list");
studentList.innerHTML = "";
students.forEach(student => {
const studentDiv = document.createElement("div");
studentDiv.textContent = `ID: ${student.id}, Name: ${student.name}, Age: ${student.age}, Major: ${student.major}`;
studentList.appendChild(studentDiv);
});
}
</script>
</body>
</html>