HTML+JS+jQuery AJAX调用后端登录并加载数据列表 - 示例代码
<!DOCTYPE html>
<html>
<head>
<title>列表页面</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>列表页面</h1>
<table id="data-table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script src="script.js"></script>
</body>
</html>
<p>$(document).ready(function() {
// 异步请求后端登录
$.ajax({
url: "backend/login.php",
method: "POST",
data: {
username: "admin",
password: "admin123"
},
success: function(response) {
// 登录成功后请求加载数据
loadData();
},
error: function(xhr, status, error) {
console.log("登录失败:" + error);
}
});
});</p>
<p>function loadData() {
// 异步请求后端加载数据
$.ajax({
url: "backend/getData.php",
method: "GET",
success: function(response) {
// 清空表格
$("#data-table tbody").empty();</p>
<pre><code> // 将数据添加到表格中
response.forEach(function(data) {
var row = $("<tr></tr>");
row.append($("<td></td>").text(data.id));
row.append($("<td></td>").text(data.name));
row.append($("<td></td>").text(data.email));
$("#data-table tbody").append(row);
});
},
error: function(xhr, status, error) {
console.log("加载数据失败:" + error);
}
</code></pre>
<p>});
}</p>
AI 优化版
原文地址:https://www.cveoy.top/t/topic/pqLa 著作权归作者所有。请勿转载和采集!