ajax学习第一天
1.客户端与服务器
1.1 服务器
- 上网过程中,负责存放和对外提供资源的电脑,叫做服务器

1.2 客户端

2. ajax
- Ajax(异步JavaScript和XML)
- 网页中利用XMLHttpRequest对象和服务器进行数据交互的方式,就是ajax
2.1 ajax的作用
2.2 ajax的应用场景




3. jQuery中的ajax
- jQuery对浏览器提供的XMLHttpRequest进行了封装,简化了ajax的使用复杂度
- jQuery中发起Ajax请求的最常用的三个方法
3.1 $.get()函数的使用
| $.get(url,[data],[callback]) |
- url:请求资源的地址,必填
- data:请求资源时携带的参数,参数类型是对象(Object)
- 请求成功时的回调函数
示例:不带参数的get请求
| <!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="./lib/jquery.js"></script> |
| </head> |
| |
| <body> |
| <button>发起不带参数的get请求</button> |
| <script> |
| $(function() { |
| $("button").on("click", function() { |
| |
| |
| $.get("http://www.liulongbin.top:3006/api/getbooks", function(res) { |
| console.log(res); |
| }); |
| }) |
| }) |
| </script> |
| </body> |
| |
| </html> |

带参数的get请求
| <!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="./lib/jquery.js"></script> |
| </head> |
| |
| <body> |
| <button>发起带参数的get请求</button> |
| <script> |
| $(function() { |
| $("button").on("click", function() { |
| |
| |
| $.get("http://www.liulongbin.top:3006/api/getbooks", { |
| id: 1 |
| }, function(res) { |
| console.log(res); |
| }); |
| }) |
| }) |
| </script> |
| </body> |
| |
| </html> |

3.2 post请求
- 专门用于发起post请求,从而向服务器提交数据
- 语法格式
| $.post(url,[data],[callback]) |
- url:提交数据的地址
- data:要提交的数据
- callback:提交成功的回调函数
示例
| <!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="./lib/jquery.js"></script> |
| </head> |
| |
| <body> |
| <button>发起post请求</button> |
| |
| <script> |
| $(function() { |
| $.post("http://www.liulongbin.top:3006/api/addbook", { |
| bookname: '斗破苍穹', |
| author: '天蚕土豆', |
| publisher: '上海图书出版社' |
| }, function(res) { |
| console.log(res); |
| }) |
| }) |
| </script> |
| </body> |
| |
| </html> |

3.3 $.ajax()函数的使用
-
功能比较综合的函数,可以GET,也可以POST
-
语法规则:
| $.ajax({ |
| type:"请求方式", |
| url:"请求地址", |
| data:{ |
| 携带的数据 |
| }, |
| |
| success:function(res){ |
| |
| } |
| }) |
示例:ajax的GET请求
| <!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="./lib/jquery.js"></script> |
| </head> |
| |
| <body> |
| <button>ajax的get请求</button> |
| |
| <script> |
| $("button").on("click", function() { |
| $.ajax({ |
| type: "GET", |
| url: "http://www.liulongbin.top:3006/api/getbooks", |
| data: { |
| id: 1 |
| }, |
| success: function(res) { |
| console.log(res); |
| } |
| }) |
| }) |
| </script> |
| </body> |
| |
| </html> |

示例:ajax实现post请求
| <!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="./lib/jquery.js"></script> |
| </head> |
| |
| <body> |
| <button>ajax的post请求</button> |
| |
| <script> |
| $(function() { |
| $("button").on("click", function() { |
| $.ajax({ |
| type: "POST", |
| url: "http://www.liulongbin.top:3006/api/addbook", |
| data: { |
| |
| bookname: '斗罗大陆', |
| author: '唐家三少1', |
| publisher: '上海图书出版社' |
| |
| }, |
| success: function(res) { |
| console.log(res); |
| } |
| }) |
| }) |
| }) |
| </script> |
| </body> |
| |
| </html> |

3.4 PostMan的使用
<Postman API Platform | Sign Up for Free>
示例:使用PostMan实现GET请求

示例:使用PostMan实现POST请求

3.5 接口文档

4. 图示管理案例

4.1 一些Bootstrap的快捷键(配合插件使用)
4.2 案例代码
| .input-group { |
| margin-right: 60px !important; |
| |
| } |
| |
| .panel-body { |
| display: flex !important; |
| justify-content: flex-start !important; |
| } |
| |
| .panel-body .btn { |
| width: 7%; |
| } |
| |
| .panel .container { |
| margin-left: 0px !important; |
| } |
| |
| .panel .table { |
| width: 96%; |
| } |
| |
| a:hover { |
| text-decoration: none !important; |
| color: red !important; |
| } |
| <!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> |
| <link rel="stylesheet" href="./lib/bootstrap.css"> |
| <link rel="stylesheet" href="./css/图示管理.css"> |
| <script src="./lib/jquery.js"></script> |
| </head> |
| |
| <body> |
| |
| |
| <div class="panel panel-success"> |
| <div class="panel-heading"> |
| <h3 class="panel-title">添加新图书</h3> |
| </div> |
| |
| |
| <div class="panel-body "> |
| |
| <div class="input-group"> |
| <span class="input-group-addon" id="sizing-addon2">图书名称</span> |
| <input type="text" class="form-control" placeholder="书名" aria-describedby="sizing-addon2"> |
| </div> |
| <div class="input-group"> |
| <span class="input-group-addon" id="sizing-addon2">作者</span> |
| <input type="text" class="form-control" placeholder="作者" aria-describedby="sizing-addon2"> |
| </div> |
| <div class="input-group"> |
| <span class="input-group-addon" id="sizing-addon2">出版社</span> |
| <input type="text" class="form-control" placeholder="出版社" aria-describedby="sizing-addon2"> |
| </div> |
| |
| |
| <button type="button" class="btn btn-primary"> 添加 </button> |
| |
| |
| </div> |
| |
| |
| |
| <div class="container"> |
| <table class="table table-hover table-striped table-bordered"> |
| <thead> |
| <tr> |
| <th>id</th> |
| <th>图书名称</th> |
| <th>作者</th> |
| <th>出版社</th> |
| <th>操作</th> |
| </tr> |
| </thead> |
| <tbody> |
| |
| |
| |
| |
| |
| |
| |
| </tbody> |
| </table> |
| </div> |
| |
| |
| |
| </div> |
| |
| <script> |
| $(function() { |
| |
| var id1; |
| |
| var books = {}; |
| getBookOnClient(id1, books); |
| |
| |
| function getBookOnServer(id1, books) { |
| console.log(id1); |
| $.get("http://www.liulongbin.top:3006/api/getbooks", { |
| id: id1 |
| }, function(res) { |
| $.extend(true, books, res); |
| |
| getBook(books); |
| }) |
| } |
| |
| |
| function getBookOnClient(id1, books) { |
| var b = $.isEmptyObject(books); |
| |
| if (b) { |
| getBookOnServer(id1, books); |
| |
| } else { |
| getBook(books); |
| |
| } |
| |
| } |
| |
| |
| function getBook(books) { |
| $(".table tbody").html(""); |
| var aData = []; |
| aData = books.data; |
| aData.sort(function(a1, b1) { |
| return b1.id - a1.id; |
| }); |
| |
| |
| |
| |
| |
| |
| |
| for (var i = 0; i < aData.length; i++) { |
| var td1 = $("<td>" + aData[i].id + "</td>"); |
| var td2 = $("<td>" + aData[i].bookname + "</td>"); |
| var td3 = $("<td>" + aData[i].author + "</td>"); |
| var td4 = $("<td>" + aData[i].publisher + "</td>"); |
| var td5 = $("<td><a href='javascript:;' id='" + i + "'>删除</a></td>"); |
| |
| var tr = $("<tr></tr>"); |
| tr.append(td1).append(td2).append(td3).append(td4).append(td5); |
| |
| $(".table tbody").prepend(tr); |
| |
| } |
| } |
| |
| |
| |
| $(".btn").on("click", function() { |
| var num = 0; |
| $(".input-group input").each(function(index, ele) { |
| if ($(ele).val() !== "") { |
| num++; |
| } else { |
| alert("请输入完整的信息") |
| return false; |
| } |
| }) |
| |
| if (num === 3) { |
| addBook($(".input-group").eq(0).children("input").val(), $(".input-group").eq(1).children("input").val(), $(".input-group").eq(2).children("input").val()) |
| |
| |
| } |
| }) |
| |
| |
| |
| |
| function addBook(bookname1, author1, publisher1) { |
| $.post("http://www.liulongbin.top:3006/api/addBook", { |
| bookname: bookname1, |
| author: author1, |
| publisher: publisher1 |
| }, function(res) { |
| |
| |
| |
| if (res.status == 201) { |
| alert("添加图书成功"); |
| var length = books.data.length; |
| console.log(length); |
| var obj = { |
| id: length + 1, |
| bookname: bookname1, |
| author: author1, |
| publisher: publisher1 |
| } |
| books.data.push(obj); |
| getBook(books); |
| |
| |
| |
| } else if (res.status == 502) { |
| alert("添加图书失败"); |
| } |
| |
| }) |
| } |
| |
| |
| $(".table tbody").on("click", "tr td a", function() { |
| var index = $(this).attr("id"); |
| console.log(index); |
| delBook(books, index); |
| }) |
| |
| function delBook(books, index) { |
| books.data.splice(index, 1); |
| getBook(books) |
| } |
| |
| console.log(books); |
| }) |
| </script> |
| |
| </body> |
| |
| </html> |


5. 聊天机器人案例

代码示例
| <!DOCTYPE html> |
| <html lang="en"> |
| |
| <head> |
| <meta charset="UTF-8" /> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| <meta http-equiv="X-UA-Compatible" content="ie=edge" /> |
| <link rel="stylesheet" href="css/reset.css" /> |
| <link rel="stylesheet" href="css/main.css" /> |
| <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script> |
| <script type="text/javascript" src="js/jquery-ui.min.js"></script> |
| <script type="text/javascript" src="js/jquery.mousewheel.js"></script> |
| <title>聊天机器人</title> |
| </head> |
| |
| <body> |
| <div class="wrap"> |
| |
| <div class="header"> |
| <h3>小思同学</h3> |
| <img src="img/person01.png" alt="icon" /> |
| </div> |
| |
| <div class="main"> |
| <ul class="talk_list" style="top: 0px;"> |
| <li class="left_word"> |
| <img src="img/person01.png" /> <span>你好</span> |
| </li> |
| <li class="right_word"> |
| <img src="img/person02.png" /> <span>你好哦</span> |
| </li> |
| </ul> |
| <div class="drag_bar" style="display: none;"> |
| <div class="drager ui-draggable ui-draggable-handle" style="display: none; height: 412.628px;"></div> |
| </div> |
| </div> |
| |
| <div class="footer"> |
| <img src="img/person02.png" alt="icon" /> |
| <input type="text" placeholder="说的什么吧..." class="input_txt" /> |
| <input type="button" value="发 送" class="input_sub" /> |
| </div> |
| </div> |
| |
| |
| |
| <audio src="" autoplay style="display: none;" class="voice"></audio> |
| <script type="text/javascript" src="js/scroll.js"></script> |
| <script src="./js/char.js"></script> |
| </body> |
| |
| </html> |
| $(function() { |
| |
| |
| resetui(); |
| |
| |
| |
| $(".footer .input_txt").on("keyup", function(e) { |
| console.log(e); |
| if (e.keyCode === 13) { |
| $(".footer .input_sub").click(); |
| } |
| }) |
| |
| |
| |
| $(".footer .input_sub").click(function() { |
| var text = $(".footer .input_txt").val().trim(); |
| if (text.length === 0) { |
| $(".footer .input_txt").val(""); |
| return false; |
| } else { |
| var li = $("<li class='right_word'><img src='img/person02.png'/><span>" + text + "</span></li>"); |
| $(".talk_list").append(li); |
| $(".footer .input_txt").val(""); |
| |
| |
| resetui(); |
| |
| |
| |
| |
| $.ajax({ |
| method: "GET", |
| url: "http://www.liulongbin.top:3006/api/robot", |
| data: { |
| spoken: text, |
| }, |
| |
| success: function(res) { |
| |
| if (res.message === "success") { |
| var reText = res.data.info.text; |
| |
| var li = $('<li class="left_word"><img src="img/person01.png" /> <span>' + reText + '</span></li>'); |
| $(".talk_list").append(li); |
| resetui(); |
| getVoice(reText); |
| } |
| |
| |
| } |
| }) |
| |
| |
| function getVoice(reText) { |
| $.ajax({ |
| method: "GET", |
| url: "http://www.liulongbin.top:3006/api/synthesize", |
| data: { |
| text: reText, |
| }, |
| |
| success: function(res) { |
| if (res.status === 200) { |
| $(".voice").attr("src", res.voiceUrl); |
| |
| } |
| } |
| }) |
| } |
| |
| |
| } |
| }) |
| |
| |
| |
| |
| |
| |
| }) |

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!