-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (66 loc) · 1.73 KB
/
index.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
function fetchData()
{
var url = "http://localhost:3000/students"
axios.get(url).then(res => {
console.log(res.data);
var str = `<html><body><table><tr><th>ID</th><th>Name</th><th>Dept</th><th>Action</th></tr>`;
res.data.forEach( student =>{
str += `<tr><td>${student.id}</td><td>${student.name}</td><td>${student.dept}</td><td><button onclick="updateData()">Edit</button><button onclick="removeData(${student.id})">Remove</button></td></td></tr>`
});
str += `</body></html>`;
document.getElementById("result").innerHTML = str;
});
}
function postData()
{
var url = "http://localhost:3000/students"
var id = document.getElementById("id").value;
var name = document.getElementById("name").value;
var dept = document.getElementById("dept").value;
const obj =
{
id:id,
name:name,
dept:dept
}
axios.post(url, obj).then( res => {
fetchData();
});
}
deleteData = () =>
{
var url = "http://localhost:3000/students/"
var id = document.getElementById("id").value;
url += id;
axios.delete(url).then(res =>
{
fetchData();
}
);
}
updateData = () =>
{
var url = "http://localhost:3000/students/"
var id = document.getElementById("id").value;
var name = document.getElementById("name").value;
var dept = document.getElementById("dept").value;
url += id;
const obj =
{
"name":name,
"dept":dept
};
axios.patch(url,obj).then( res =>{
fetchData();
});
}
function removeData(id)
{
var url = "http://localhost:3000/students/"
url += id;
axios.delete(url).then(res =>
{
fetchData();
}
);
}