-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
56 lines (54 loc) · 2.38 KB
/
Copy pathscript.js
File metadata and controls
56 lines (54 loc) · 2.38 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
var API_ENDPOINT = "https://ulmgtymvh8.execute-api.ap-south-1.amazonaws.com/Data";
// AJAX POST request to save student data
document.getElementById("savestudent").onclick = function(event){
event.preventDefault(); // Prevent the form from submitting
var inputData = {
"studentid": document.getElementById('studentId').value,
"name": document.getElementById('firstName').value + ' ' + document.getElementById('middleName').value + ' ' + document.getElementById('lastName').value,
"email": document.getElementById('email').value,
"gender": document.getElementById('gender').value,
"course": document.getElementById('courseList').value,
};
$.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 (xhr, status, error) {
console.error("Error saving student data:", error);
console.error("Status:", status);
console.error("Response:", xhr.responseText);
alert("Error saving student data.");
}
});
}
// AJAX GET request to retrieve all students
document.getElementById("getstudents").onclick = function(event){
event.preventDefault(); // Prevent the form from submitting
$.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['gender'] + "</td> \
<td>" + data['email'] + "</td> \
<td>" + data['course'] + "</td> \
</tr>");
});
},
error: function (xhr, status, error) {
console.error("Error retrieving student data:", error);
console.error("Status:", status);
console.error("Response:", xhr.responseText);
alert("Error retrieving student data.");
}
});
}