发表于: 2023-04-04 20:40:11
0 255
今天的ajax知识点:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="../bootstrap/js/jquery-3.5.1/jquery-3.5.1.min.js"></script>
<title>Document</title>
</head>
<body>
<button id="btnPOST">发起POST请求</button>
<script>
$(function() {
$('#btnPOST').on('click', function() {
$.ajax({
type: 'POST',
url: 'http://www.liulongbin.top:3006/api/addbook',
date: {
bookname: '史记',
author: '司马迁',
id: 230,
publisher: '上海图书出版社'
},
success: function(res) {
console.log(res);
}
})
})
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../bootstrap/js/jquery-3.5.1/jquery-3.5.1.min.js"></script>
</head>
<body>
<button id="btnGet">发起GET请求</button>
<script>
$(function() {
$('#btnGet').on('click', function() {
$.ajax({
type: 'GET',
url: 'http://www.liulongbin.top:3006/api/getbooks',
date: {
id: 1
},
success: function(res) {
console.log(res);
}
})
})
})
</script>
</body>
</html>
图书馆案例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图书管理</title>
<link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css">
<script src="../bootstrap/js/jquery-3.5.1/jquery-3.5.1.min.js"></script>
</head>
<body style="padding: 15px;">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">添加新图书</h3>
</div>
<div class="panel-body form-inline">
<div class="input-group">
<div class="input-group-addon">书名</div>
<input type="text" class="form-control" id="bookname" placeholder="请输入书名">
</div>
<div class="input-group">
<div class="input-group-addon">作者</div>
<input type="text" class="form-control" id="Author" placeholder="请输入作者">
</div>
<div class="input-group">
<div class="input-group-addon">出版社</div>
<input type="text" class="form-control" id="publisher" placeholder="请输入出版社">
</div>
<button type="button" class="btn btn-primary">添加</button>
</div>
</div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>出版社</th>
<th>操作</th>
</tr>
</thead>
<tbody id="db">
</tbody>
</table>
<script>
$(function(){
//获取图书列表get
function getBookList(){ //封装函数getBookList(),以便后续的重新渲染
$.get('http://www.liulongbin.top:3006/api/getbooks',function(res){
// console.log(res);
var rows = []; //声明一个空数组,用来存储从API上获取的数据
if(res.status !== 200){
return alert('获取数据失败');
}else{
$.each(res.data,function(i,n){ //遍历数组获得到ID值和数据生成代码放到数组里
rows.push(`<tr><td>${n.id}</td><td>${n.bookname}</td><td>${n.author}</td><td>${n.publisher}</td><td><a href='javascript:;' class='del' date-id=${n.id}>删除</a></td></tr>`);
})
}
$("#db").empty().append(rows.join('$')); //获取到存放上面代码的父亲,清空之后,最新数据依次放到最前
})
}
getBookList(); //浏览器一打开,就调用函数渲染一边
//删除del
//代理绑定
$("tbody").on("click",".del",function(){
var id = $(this).attr("date-id"); //自定义一个ID属性,获取当前点击删除的父亲元素的索引值
// console.log(id);
$.get('http://www.liulongbin.top:3006/api/delbook',
{id:id},
function(res){
if(res.status !== 200){
return alert('删除图书失败');
}else{
getBookList();//从新渲染
}
})
})
//添加图书 post
$("button").on("click",function(){
// trim() 去除字符串两端的空格
var bookname = $("#bookname").val().trim();
var author = $("#Author").val().trim();
var publisher = $("#publisher").val().trim();
if(bookname.length <= 0 || author.length <= 0 || publisher.length <= 0){
alert('添加图书失败,请输入完整的图书信息');
}
// alert('成功');
$.post('http://www.liulongbin.top:3006/api/addbook',
{bookname : bookname,author :author,publisher :publisher},
function(res){
if(res.status !== 201){
return alert('添加图书失败');
}
getBookList();//从新渲染
$("#bookname").val("");
$("#Author").val("");
$("#publisher").val("");
})
})
})
</script>
</body>
</html>
评论