-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
47 lines (45 loc) · 1.51 KB
/
scripts.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
// Add your API endpoint here
var API_ENDPOINT = "YOUR_API_ENDPOINT";
// AJAX POST request to save student data
document.getElementById("savestudent").onclick = function(){
var inputData = {
"studentid": $('#studentid').val(),
"name": $('#name').val(),
"class": $('#class').val(),
"age": $('#age').val()
};
$.ajax({
url: API_ENDPOINT,
type: 'POST',
data: JSON.stringify(inputData),
contentType: 'application/json; charset=utf-8',
success: function (response) {
document.getElementById("studentSaved").innerHTML = "Student Data Saved!";
},
error: function () {
alert("Error saving student data.");
}
});
}
// AJAX GET request to retrieve all students
document.getElementById("getstudents").onclick = function(){
$.ajax({
url: API_ENDPOINT,
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function (response) {
$('#studentTable tr').slice(1).remove();
jQuery.each(response, function(i, data) {
$("#studentTable").append("<tr> \
<td>" + data['studentid'] + "</td> \
<td>" + data['name'] + "</td> \
<td>" + data['class'] + "</td> \
<td>" + data['age'] + "</td> \
</tr>");
});
},
error: function () {
alert("Error retrieving student data.");
}
});
}