Skip to content

Commit ca8685f

Browse files
committed
use ajax method
1 parent 09a13dc commit ca8685f

File tree

3 files changed

+117
-36
lines changed

3 files changed

+117
-36
lines changed

5.rest-api-express/public/js/1.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

5.rest-api-express/public/js/app.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
3+
$(document).ready(function () {
4+
5+
$("#btn_adduser").click(function () {
6+
// alert('call ajax method success!');
7+
$.post("/addUser", function (data, status) {
8+
// alert("Data: " + data + "\nStatus: " + status);
9+
10+
$("#result").fadeOut(1000)
11+
12+
var obj = JSON.parse(data);
13+
console.log(obj);
14+
15+
var trlist = '';
16+
17+
$.each(obj, function (i, item) {
18+
console.log(item.name);
19+
20+
trlist += `<tr>
21+
<td>${item.name}</td>
22+
<td>${item.password}</td>
23+
<td>${item.profession}</td>
24+
<td>${item.id}</td>
25+
</tr>`;
26+
27+
// alert(trlist);
28+
29+
});
30+
31+
32+
var table = `<table style="width:100%">
33+
<tr class="table-header">
34+
<td>name</td>
35+
<td>password</td>
36+
<td>professional</td>
37+
<td>id</td>
38+
</tr>
39+
40+
41+
42+
${trlist}
43+
44+
</table>`
45+
46+
47+
$("#test").fadeOut(1000);
48+
$('#test').html(table);
49+
$("#test").fadeIn(1000);
50+
51+
var jsontext = JSON.stringify(JSON.parse(data), undefined, 2);
52+
53+
$('#result').text(jsontext);
54+
$("#result").fadeIn(1000);
55+
});
56+
57+
58+
59+
// fetch('/addUser', {
60+
// method: 'POST',
61+
// headers: {
62+
// 'Accept': 'application/json',
63+
// 'Content-Type': 'application/json'
64+
// },
65+
// body: JSON.stringify({ "id": 78912 })
66+
// })
67+
// .then(response => response.json())
68+
// .then(response => console.log(JSON.stringify(response)))
69+
70+
71+
72+
});
73+
74+
});

5.rest-api-express/views/index.html

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<title>Document</title>
88

99
<script src="/public/js/jquery-3.6.3.min.js"></script>
10+
11+
<script src="/public/js/app.js"></script>
1012
<link rel="/public/css/app.css" href="styles.css">
1113

1214
<style type="text/css">
@@ -34,45 +36,26 @@
3436
background-color: aqua;
3537
padding: 30px;
3638
}
37-
</style>
38-
39-
<script type="text/javascript">
40-
$(document).ready(function () {
41-
42-
$("#btn_adduser").click(function () {
43-
// alert('call ajax method success!');
44-
$.post("/addUser", function (data, status) {
45-
alert("Data: " + data + "\nStatus: " + status);
46-
47-
// $("#result").fadeOut(1000)
48-
// $('#result').text(JSON.stringify(JSON.parse(data), undefined, 2));
49-
// $("#result").fadeIn(1000);
50-
});
51-
52-
5339

54-
// fetch('/addUser', {
55-
// method: 'POST',
56-
// headers: {
57-
// 'Accept': 'application/json',
58-
// 'Content-Type': 'application/json'
59-
// },
60-
// body: JSON.stringify({ "id": 78912 })
61-
// })
62-
// .then(response => response.json())
63-
// .then(response => console.log(JSON.stringify(response)))
64-
65-
66-
67-
});
68-
69-
});
40+
table {
41+
font-family: arial, sans-serif;
42+
border-collapse: collapse;
43+
width: 100%;
44+
}
7045

71-
46+
td,
47+
th {
48+
border: 1px solid #dddddd;
49+
text-align: left;
50+
padding: 8px;
51+
}
7252

53+
tr:nth-child(even) {
54+
background-color: #dddddd;
55+
}
56+
</style>
7357

7458

75-
</script>
7659

7760

7861
</head>
@@ -90,7 +73,32 @@
9073
<p>Ajax action</p>
9174
<input type="submit" id="btn_adduser" value="Submit Ajax">
9275
<p>Result</p>
93-
<pre id="result" class="result"></pre>
76+
77+
<pre id="result" class="result">
78+
79+
</pre>
80+
81+
<table style="width:100%">
82+
<tr class="table-header">
83+
<td>name</td>
84+
<td>password</td>
85+
<td>professional</td>
86+
<td>id</td>
87+
</tr>
88+
89+
<tr>
90+
<td>1</td>
91+
<td>2</td>
92+
<td>3</td>
93+
<td>4</td>
94+
</tr>
95+
96+
97+
</table>
98+
</br></br></br>
99+
100+
<div id="test"></div>
101+
94102
</div>
95103

96104
</div>

0 commit comments

Comments
 (0)