ajax学习笔记
服务器相关基本概念
服务器:对外存放和提供资源的电脑
客户端:上网过程,负责获取和消费资源的电脑。
URL地址
URL地址是什么
统一资源定位符,标识互联网上每个资源的唯一存放位置,浏览器只有通过url地址才能正确定位资源的存放位置,从而访问到对应的资源。
URL的组成部分
URL地址一般由三部分组成:
- 客户端与服务器端之间的通信协议
- 存有该资源的服务器名称
- 资源在服务器上具体的存放位置
http: //www.baidu.com/具体位置
客户端和服务器通信的过程
请求(客户端)–处理(服务器)–响应(服务器)
服务器对外提供了哪些资源
文字,图片,音乐,视频,数据等
对于一个网站来说,HTML是网页的骨架,CSS是网页的颜值,javascript是网页的行为,数据,则是网页的灵魂。
什么是ajax
ajax的基本概念
Ajax的全称是Asynchronous Javascript And XML(异步的JavaScript和xml),即在网页中利用XMLHttpRequest对象和服务器进行数据交互的方式。
为什么要使用Ajax
为了实现网页与服务器之间的数据交互。
ajax的应用场景
用户名检测:注册用户时,通过ajax的形式,动态的检测用户名是否被占用。
搜索提示:当输入搜索关键字时,通过ajax的形式,动态加载搜索提示列表。
数据的分页显示:当点击页码的时候,通过ajax的形式,根据页码值动态刷新表格的数据。
数据的增删改查:数据的增删改查操作,都需要通过ajax来实现数据的交互
jQuery中的Ajax函数的使用
初识jQuery中的Ajax
浏览器中提供的XMLHttpRequest用法比较复杂,所以用jQuery对XMLHttpRequest进行封装,jQuery中发起Ajax请求最常用的三个方法如下:
-
.
g
e
t
(
)
:向服务器请求资源语法:
.get():向服务器请求资源 语法:
.get():向服务器请求资源语法:.get(url,[data],[callback])(方括号中的两个数据代表可选)
- . p o s t ( ) :向服务器提交数据语法: .post():向服务器提交数据 语法: .post():向服务器提交数据语法:.post(url,[data],[callback])(方括号中的两个数据代表可选)
- $.ajax():
语法:
$.ajax({
type:'',//请求的方式,如get或post
url:'',//请求的url地址
data:',',这次请求要携带的数据
success:function(res){//请求成功之后的回调函数
}
})
$.get()发起不带参数的请求
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="./lib/jquery.js"></script>
</head>
<body>
<button id="btnGet">发起不带参数的get请求</button>
<script>
$(function() {
$('#btnGet').on('click', function() {
alert("绑好了")
$.get('http://www.liulongbin.top:3006/api/getbooks', function(res) {
console.log(res);
})
})
})
</script>
</body>
</html>
$.get()发起带参数的请求
<script>
$(function(){
$('#btnGet').on('click',function(){
$.get('http://www.liulongbin.top:3006/api/getbooks',{id:1},function(res){
console.log(res)
})
})
})
</script>
$.post()提交数据到服务器
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>使用post提交数据</title>
<script src="./lib/jquery.js"></script>
</head>
<body>
<button id="btnPost">使用post提交数据</button>
<script>
$(function(){
$("#btnPost").on("click",function(){
$.post('http://www.liulongbin.top:3006/api/addbook',
{bookname:'疏忽转',author:'小娜扎',publisher:'t天津出版社'},function(res){
console.log(res)
})
})
})
</script>
</body>
</html>
使用$.ajax()发起get请求
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="./lib/jquery.js"></script>
</head>
<body>
<button id="ajaxGet">使用ajax函数发送get请求</button>
<script>
$(function(){
$('#ajaxGet').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>
<head>
<meta charset="utf-8">
<title></title>
<script src="./lib/jquery.js"></script>
</head>
<body>
<button id="ajaxPost">使用ajax函数发送post请求</button>
<script>
$(function(){
$('#ajaxPost').on('click',function(){
$.ajax({
type:'post',
url:'http://www.liulongbin.top:3006/api/addbook',
data:{
bookname:'世纪',
author:'司马',
publisher:'图书出版社'
},
success:function(res){
console.log(res)
}
})
})
})
</script>
</body>
</html>
接口和接口文档的概念
接口:使用ajax请求数据时,被请求的url地址,就叫做数据接口,每个接口必须要有请求方式,即get,post。
接口文档:调用接口的依赖,包含了对接口URL,参数及输出内容的说明。
接口文档的组成部分:
- 接口名称:对接口的简单说明(如登录接口,注册接口)。
- 接口URL:接口的调用地址。
- 调用方式:如get,post。
- 参数格式:接口需要传递的参数,每个参数必须包含参数名称,参数类型,是否必选,参数说明。
- 响应格式:接口返回值的详细描述,一般包含数据名称,数据类型,说明三项内容
- 返回示例:通过对象的形式,列举服务器返回数据的结构
案例
图书管理
layui+jquery
这里有点问题我始终没搞懂,在我的学习历程中,添加的信息需要放在表单里面提交,在这里我在添加部分加了form标签反而添加不了,也不报错,有懂得大哥在下面回复一下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- <link rel="stylesheet" href="../lib/bootstrap.css" /> -->
<script src="../lib/jquery.js"></script>
<link rel="stylesheet" href="../layui/css/layui.css">
</head>
<body style="padding: 15px;">
<div class="layui-card">
<div class="layui-card-header" style="background-color: #01AAED;color: white;">添加新图书</div>
<div class="layui-card-body">
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label" style="background-color: #D3D4D3;width: 30px">书名</label>
<div class="layui-input-inline" style="width: 150px;">
<input type="text" id="bookname" name="bookname" required lay-verify="required"
placeholder="请输入书名" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label" style="background-color: #D3D4D3;width: 30px">作者</label>
<div class="layui-input-inline" style="width: 150px;">
<input type="text" id="author" name="author" required lay-verify="required"
placeholder="请输入作者" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label" style="background-color: #D3D4D3;width: 45px">出版社</label>
<div class="layui-input-inline" style="width: 150px;">
<input type="text" id="publisher" name="publisher" required lay-verify="required"
placeholder="请输入出版社" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-inline">
<div class="layui-input-block">
<button class="layui-btn" id="btnAdd" lay-submit lay-filter="formDemo"
style="background-color: #01AAED">添加</button>
</div>
</div>
</div>
</div>
</div>
<table class="layui-table">
<h1 align="center">图书列表</h1>
<colgroup>
<col width="150">
<col width="200">
<col>
</colgroup>
<thead>
<tr>
<th>id</th>
<th>书名</th>
<th>作者</th>
<th>出版社</th>
<th>操作</th>
</tr>
</thead>
<tbody id="tb"></tbody>
</table>
<script>
$(function() {
//获取图书列表数据
function getBookList() {
// alert("哈哈")
$.get('http://www.liulongbin.top:3006/api/getbooks', function(res) {
// console.log(res)
if (res.status !== 200) return alert("获取数据失败!")
var rows = [];
$.each(res.data, function(i, item) {
rows.push('<tr><td>' + item.id + '</td><td>' + item.bookname + '</td><td>' +
item.author + '</td><td>' + item.publisher +
'</td><td><a class="del" data-name="' + item.bookname +
'" data-id="' + item.id + '" href="javascript:;">删除</a></td></tr>')
})
$('#tb').empty().append(rows.join(''))
})
}
getBookList();
//删除图书
$('#tb').on('click', '.del', function() {
var bookname = $(this).attr('data-name')
var id = $(this).attr('data-id')
// console.log(id)
$.get('http://www.liulongbin.top:3006/api/delbook', {
id: id
}, function(res) {
alert("确定删除第《" + bookname + "》本书吗")
if (res.status !== 200) return alert('删除失败!')
getBookList()
// alert("删除成功")
})
})
//添加图书
$("#btnAdd").on('click', function() {
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) {
return 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>
聊天机器人
<!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">
<!-- 头部 Header 区域 -->
<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>
</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" id="input" placeholder="说的什么吧..." class="input_txt" />
<input type="button" id="btnSend" value="发 送" class="input_sub" />
</div>
</div>
<!-- 播放语音 -->
<audio src="" id="voice" autoplay style="display: none;"></audio>
<!-- 滚动条的相关文件 -->
<script type="text/javascript" src="js/scroll.js"></script>
<!-- 用户输入内容实现聊天的相关文件 -->
<script src="./js/chat.js"></script>
</body>
</html>
$(function() {
// 初始化右侧滚动条
// 这个方法定义在scroll.js中
resetui()
//将用户输入的内容渲染到聊天窗口中
//为发送按钮绑定鼠标点击事件
$('#btnSend').on('click',function(){
var text = $('#input').val().trim()
if(text.length<=0){
return $('#input').val('')
}
//如果用户输入了聊天内容,就追加到页面上显示
$('.talk_list').append('<li class="right_word"><img src="img/person02.png"/><span>'+text+'</span></li>')
$('#input').val('')//清空输入框
//重置滚动条的位置
resetui()
//获取机器人的响应内容
getMsg(text)
})
//获取聊天机器人的内容
function getMsg(text){
$.ajax({
method:'get',
url:'http://www.liulongbin.top:3006/api/robot',
data:{
spoken:text
},
success:function(res){
console.log(res)
var msg = res.data.info.text
$('.talk_list').append('<li class="left_word"><img src="img/person01.png" /> <span>'+msg+'</span></li>')
resetui()
//转语音
getVoice(msg)
}
})
}
//把文字转语音播放
function getVoice(msg){
$.ajax({
type:'get',
url:'http://www.liulongbin.top:3006/api/synthesize',
data:{
text:msg
},
success:function(res){
console.log(res)
if(res.status ===200){
//播放语音
$('#voice').attr('src',res.voiceUrl)
}
}
})
}
//通过点击回车实现发送消息
//为文本框绑定keyup事件,获取回车键的keyCode码
$('#input').on('keyup',function(e){
e.keyCode//获取当前按键的编码
if(e.keyCode ===13){
//如果点击了回车,则触发发送的点击事件
$('#btnSend').click()
}
})
})
图书管理和机器人案例完整代码地址:
https://download.csdn.net/download/weixin_46002862/86263293