-
Notifications
You must be signed in to change notification settings - Fork 0
/
playerform.js
67 lines (61 loc) · 1.72 KB
/
playerform.js
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
$(document).ready(() => {
$("#insert").on("click", (event) => Insert(event));
$("#update").on("click", (event) => Update(event));
$("#delete").on("click", (event) => Delete(event));
});
function Insert(event) {
// 避免重新整理清除資料
event.preventDefault();
$.ajax({
url: "/api/insertPlayer",
method: "POST",
data: { id: $("#id").val(), name: $("#name").val() },
success: (data) => {
$("#result").html(`<p>Player Created: Name = ${data.name}</p>`);
},
error: (xhr) => {
$("#result").html(`<p>Error: ${xhr.responseJSON.error}</p>`);
},
});
}
function Update(event) {
// 避免重新整理清除資料
event.preventDefault();
$.ajax({
url: "/api/updatePlayer",
method: "PUT",
data: { id: $("#id").val(), name: $("#name").val() },
success: (data) => {
$("#result").html(`<p>Player AfterUpdate: Name = ${data.name}</p>`);
},
error: (xhr) => {
console.log("Error response:", xhr); // Log the error response for debugging
$("#result").html(
`<p>Error: ${
xhr.responseJSON ? xhr.responseJSON.error : "Unknown error"
}</p>`
);
},
});
}
function Delete(event) {
// 避免重新整理清除資料
event.preventDefault();
$.ajax({
url: "/api/deletePlayer",
method: "DELETE",
// 送回indexDatabase.js
data: { id: $("#id").val() },
success: (data) => {
$("#result").html(`<p>Player ${data.id} delete success</p>`);
},
error: (xhr) => {
console.log("Error response:", xhr); // Log the error response for debugging
$("#result").html(
`<p>Error: ${
xhr.responseJSON ? xhr.responseJSON.error : "Unknown error"
}</p>`
);
},
});
}