![](https://img-blog.csdnimg.cn/img_convert/039e34aff58c1c5f185846ec02d4910f.png)
![](https://img-blog.csdnimg.cn/img_convert/2f78da91d67d6f4d10796eea04962cb5.png)
1.1-AJAX入门与axios使用
![](https://img-blog.csdnimg.cn/img_convert/5cb30a8eab8f50c74ae3cffd54e455aa.png)
![](https://img-blog.csdnimg.cn/img_convert/3a54d8ca3b263109c17ca8fb6f9d3843.png)
![](https://img-blog.csdnimg.cn/img_convert/6533e7a4b73a274774a11b9918d5ecc7.png)
![](https://img-blog.csdnimg.cn/img_convert/d8698dd857c26a47ed544a68417ded4f.png)
![](https://img-blog.csdnimg.cn/img_convert/45814b078cfc4fea0c486dd6d6343999.png)
![](https://img-blog.csdnimg.cn/img_convert/7d7b14e051a2747cadd80e8f980fbf1d.png)
![](https://img-blog.csdnimg.cn/img_convert/837e75d77f9041205ffeb3e7a2d6a02a.png)
![](https://img-blog.csdnimg.cn/img_convert/8c355c3be17da1d8974e233d974ec789.png)
![](https://img-blog.csdnimg.cn/img_convert/4062a80109cb4dc5ffddaa2815d78c4a.png)
1.2-认识URL
![](https://img-blog.csdnimg.cn/img_convert/c36e0e449a042a33c5cc6ba303960bec.png)
![](https://img-blog.csdnimg.cn/img_convert/607886e7cc834e46d99e302d0781bbd3.png)
![](https://img-blog.csdnimg.cn/img_convert/fb1cb760fdd2148930c6ba3c6c92b7bd.png)
![](https://img-blog.csdnimg.cn/img_convert/daa2f0059978badbc7f5cb16e00084ba.png)
![](https://img-blog.csdnimg.cn/img_convert/f7cc6756c95d9eb1a65b3ec199a11719.png)
![](https://img-blog.csdnimg.cn/img_convert/1ad0d7fd71487dec42f9eb9fc2d1cfd0.png)
![](https://img-blog.csdnimg.cn/img_convert/a3824588a2c319c53a3b7b7583b322d5.png)
![](https://img-blog.csdnimg.cn/img_convert/33cb69198dedf77e8adfd52862aea2bd.png)
![](https://img-blog.csdnimg.cn/img_convert/d498f98cc41f677024b8934c68718c7b.png)
1.3-查询参数
![](https://img-blog.csdnimg.cn/img_convert/583007f462c11a70711cd1aa1b480543.png)
![](https://img-blog.csdnimg.cn/img_convert/d9982f9e654d2676fa7ed1cca2e0b5bc.png)
![](https://img-blog.csdnimg.cn/img_convert/8c5550d424e9497044aad4ef112b9c29.png)
![](https://img-blog.csdnimg.cn/img_convert/457207ef148d9917aa831f55e2413919.png)
![](https://img-blog.csdnimg.cn/img_convert/1788c8903740a451a021260a2ca71ae4.png)
1.4-案例_地区查询
![](https://img-blog.csdnimg.cn/img_convert/0e9798b0c3b12a9fbae8c2b30e0ff6d9.png)
![](https://img-blog.csdnimg.cn/img_convert/b7c28eda1831848f86d2bbf04bb40c06.png)
![](https://img-blog.csdnimg.cn/img_convert/c5002a98c6001216e3e3e4d0eb2e65ad.png)
![](https://img-blog.csdnimg.cn/img_convert/670e5b3fd0594fe211daee1ae058da8f.png)
![](https://img-blog.csdnimg.cn/img_convert/ac906d995ec87f1027fa363204bf7c40.png)
![](https://img-blog.csdnimg.cn/img_convert/61a91d9e514532c05fb25d82e27b75a4.png)
![](https://img-blog.csdnimg.cn/img_convert/1a592d19aa0d04b42a34da174b18fa79.png)
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
/*
获取地区列表: http://hmajax.itheima.net/api/area
查询参数:
pname: 省份或直辖市名字
cname: 城市名字
*/
// 目标: 根据省份和城市名字, 查询地区列表
// 1. 查询按钮-点击事件
document.querySelector('.sel-btn').addEventListener('click', () => {
// 2. 获取省份和城市名字
let pname = document.querySelector('.province').value
let cname = document.querySelector('.city').value
// 3. 基于axios请求地区列表数据
axios({
url: 'http://hmajax.itheima.net/api/area',
params: {
pname,
cname
}
}).then(result => {
// console.log(result)
// 4. 把数据转li标签插入到页面上
let list = result.data.list
console.log(list)
let theLi = list.map(areaName => `<li class="list-group-item">${areaName}</li>`).join('')
console.log(theLi)
document.querySelector('.list-group').innerHTML = theLi
})
})
</script>
1.5-常用请求方法和数据提交
![](https://img-blog.csdnimg.cn/img_convert/96e1f44cf538e5aecd1ca6e211c69756.png)
![](https://img-blog.csdnimg.cn/img_convert/17a6fea557e8f2faf25068f1b72219ff.png)
![](https://img-blog.csdnimg.cn/img_convert/52984a46960acdae6ff4b391754eeb3b.png)
![](https://img-blog.csdnimg.cn/img_convert/846992b0a1d5346ced51ea3f6a573c9d.png)
![](https://img-blog.csdnimg.cn/img_convert/7f8bcd77aa0d9689616d8b64444c74c3.png)
![](https://img-blog.csdnimg.cn/img_convert/6bb76d0efdc62f2b659b2876bfc51048.png)
1.6-axios错误处理
![](https://img-blog.csdnimg.cn/img_convert/82c83287e145c874602278d645f9d5ce.png)
![](https://img-blog.csdnimg.cn/img_convert/b65ed17e817b10d16603a0859037a1c5.png)
![](https://img-blog.csdnimg.cn/img_convert/4263fd69952498abe4fec2ba429a11dc.png)
![](https://img-blog.csdnimg.cn/img_convert/614e1c52234d14c14217d20184a7cb70.png)
<button class="btn">注册用户</button>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
/*
注册用户: http://hmajax.itheima.net/api/register
请求方法: POST
参数名:
username: 用户名 (中英文和数字组成, 最少8位)
password: 密码 (最少6位)
目标: 点击按钮, 通过axios提交用户和密码, 完成注册
需求: 使用axios错误处理语法, 拿到报错信息, 弹框反馈给用户
*/
document.querySelector('.btn').addEventListener('click', () => {
axios({
url: 'http://hmajax.itheima.net/api/register',
method: 'post',
data: {
username: 'itheima007',
password: '7654321'
}
}).then(result => {
// 成功
console.log(result)
}).catch(error => {
// 失败
// 处理错误信息
console.log(error)
console.log(error.response.data.message)
alert(error.response.data.message)
})
})
</script>
1.7-HTTP协议请求报文
![](https://img-blog.csdnimg.cn/img_convert/23d595166cc5c4c7d72c2ef9114f8c9a.png)
![](https://img-blog.csdnimg.cn/img_convert/bc7965366c6d545bdeed441a9c59bab8.png)
![](https://img-blog.csdnimg.cn/img_convert/4a4a8be900ff617672b665c1475f4e66.png)
![](https://img-blog.csdnimg.cn/img_convert/a8446068d7cfe573e078eaeb78e61371.png)
![](https://img-blog.csdnimg.cn/img_convert/2b5f0238ed644bbbd31b01318c3724a3.png)
![](https://img-blog.csdnimg.cn/img_convert/72cb0d01e111de2af0451f05a0c4fad4.png)
1.8-请求报文-错误排查
![](https://img-blog.csdnimg.cn/img_convert/0d2b68c98db09a1a1e72598ab4e34f15.png)
![](https://img-blog.csdnimg.cn/img_convert/2563eacb8a8bbc9de6e90554a6fd4732.png)
![](https://img-blog.csdnimg.cn/img_convert/3f1372b24e634d17d796eb603773be76.png)
1.9-HTTP协议响应报文
![](https://img-blog.csdnimg.cn/img_convert/fb12c4c2c96635d21737d278f1900cc1.png)
![](https://img-blog.csdnimg.cn/img_convert/e3635518ffe55633c0559108f365b021.png)
![](https://img-blog.csdnimg.cn/img_convert/4b2d6ae740acd33817ef4009fcc7f56c.png)
![](https://img-blog.csdnimg.cn/img_convert/52667668a3d154be33f5f6a10a03956e.png)
![](https://img-blog.csdnimg.cn/img_convert/fc9138b1431ca020fd1adfde7ca82435.png)
![](https://img-blog.csdnimg.cn/img_convert/de3b5fb470e44119b4ad3f712b5e8bcc.png)
1.10-接口文档
![](https://img-blog.csdnimg.cn/img_convert/f505824217a5d5bc2840ca3f8822dc64.png)
接口用例 | Apifox 帮助文档
![](https://img-blog.csdnimg.cn/img_convert/48ca076857c0bee3ca4969ceac0d112f.png)
<body>
<button class="btn">用户登录</button>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
// 用户注册
// axios({
// url: 'http://hmajax.itheima.net/api/register',
// method: 'post',
// data: {
// username: 'itheima007',
// password: '7654321'
// }
// })
document.querySelector('.btn').addEventListener('click', () => {
// 用户登录
axios({
url: 'http://hmajax.itheima.net/api/login',
method: 'post',
data: {
username: 'itheima007',
password: '7654321'
}
})
})
</script>
</body>
![](https://img-blog.csdnimg.cn/img_convert/a1031b3bf7531a606668c848507b61b5.png)
![](https://img-blog.csdnimg.cn/img_convert/53c73347d9a36103fe028dbd20bc2b94.png)
1.11-案例_登录
![](https://img-blog.csdnimg.cn/img_convert/b84bf65c91ea34fb00987f2b9aea9efb.png)
![](https://img-blog.csdnimg.cn/img_convert/abadd5ca40310b78799804b94e3a6808.png)
![](https://img-blog.csdnimg.cn/img_convert/007a68618143cc6d867044579ff51367.png)
<!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>
<!-- 引入bootstrap.css -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css">
<!-- 公共 -->
<style>
html,
body {
background-color: #EDF0F5;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.container {
width: 520px;
height: 540px;
background-color: #fff;
padding: 60px;
box-sizing: border-box;
}
.container h3 {
font-weight: 900;
}
</style>
<!-- 表单容器和内容 -->
<style>
.form_wrap {
color: #8B929D !important;
}
.form-text {
color: #8B929D !important;
}
</style>
<!-- 提示框样式 -->
<style>
.alert {
transition: .5s;
opacity: 0;
}
.alert.show {
opacity: 1;
}
</style>
</head>
<body>
<div class="container">
<h3>欢迎-登录</h3>
<!-- 登录结果-提示框 -->
<div class="alert alert-success" role="alert">
提示消息
</div>
<!-- 表单 -->
<div class="form_wrap">
<form>
<div class="mb-3">
<label for="username" class="form-label">账号名</label>
<input type="text" class="form-control username">
</div>
<div class="mb-3">
<label for="password" class="form-label">密码</label>
<input type="password" class="form-control password">
</div>
<button type="button" class="btn btn-primary btn-login"> 登 录 </button>
</form>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
// 目标1:点击登录时,用户名和密码长度判断,并提交数据和服务器通信
// 1.1 登录-点击事件
document.querySelector('.btn-login').addEventListener('click', () => {
// 1.2 获取用户名和密码
const username = document.querySelector('.username').value
const password = document.querySelector('.password').value
// console.log(username, password)
// 1.3 判断长度
if (username.length < 8) {
console.log('用户名必须大于等于8位')
return // 阻止代码继续执行
}
if (password.length < 6) {
console.log('密码必须大于等于6位')
return // 阻止代码继续执行
}
// 1.4 基于axios提交用户名和密码
// console.log('提交数据到服务器')
axios({
url: 'http://hmajax.itheima.net/api/login',
method: 'POST',
data: {
username,
password
}
}).then(result => {
console.log(result)
console.log(result.data.message)
}).catch(error => {
console.log(error)
console.log(error.response.data.message)
})
})
</script>
</body>
</html>
1.12-案例登录提示消息
![](https://img-blog.csdnimg.cn/img_convert/7702ccddf42e4dd224d6df589069c172.png)
![](https://img-blog.csdnimg.cn/img_convert/4054ecd95acecaccb663059bbb1cafd0.png)
<!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>
<!-- 引入bootstrap.css -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css">
<!-- 公共 -->
<style>
html,
body {
background-color: #EDF0F5;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.container {
width: 520px;
height: 540px;
background-color: #fff;
padding: 60px;
box-sizing: border-box;
}
.container h3 {
font-weight: 900;
}
</style>
<!-- 表单容器和内容 -->
<style>
.form_wrap {
color: #8B929D !important;
}
.form-text {
color: #8B929D !important;
}
</style>
<!-- 提示框样式 -->
<style>
.alert {
transition: .5s;
opacity: 0;
}
.alert.show {
opacity: 1;
}
</style>
</head>
<body>
<div class="container">
<h3>欢迎-登录</h3>
<!-- 登录结果-提示框 -->
<div class="alert alert-success" role="alert">
提示消息
</div>
<!-- 表单 -->
<div class="form_wrap">
<form>
<div class="mb-3">
<label for="username" class="form-label">账号名</label>
<input type="text" class="form-control username">
</div>
<div class="mb-3">
<label for="password" class="form-label">密码</label>
<input type="password" class="form-control password">
</div>
<button type="button" class="btn btn-primary btn-login"> 登 录 </button>
</form>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
// 目标1:点击登录时,用户名和密码长度判断,并提交数据和服务器通信
// 目标2:使用提示框,反馈提示消息
// 2.1 获取提示框
const myAlert = document.querySelector('.alert')
/**
* 2.2 封装提示框函数,重复调用,满足提示需求
* 功能:
* 1. 显示提示框
* 2. 不同提示文字msg,和成功绿色失败红色isSuccess(true成功,false失败)
* 3. 过2秒后,让提示框自动消失
*/
function alertFn(msg, isSuccess) {
// 1> 显示提示框
myAlert.classList.add('show')
// 2> 实现细节
myAlert.innerText = msg
const bgStyle = isSuccess ? 'alert-success' : 'alert-danger'
myAlert.classList.add(bgStyle)
// 3> 过2秒隐藏
setTimeout(() => {
myAlert.classList.remove('show')
// 提示:避免类名冲突,重置背景色
myAlert.classList.remove(bgStyle)
}, 2000)
}
// 1.1 登录-点击事件
document.querySelector('.btn-login').addEventListener('click', () => {
// 1.2 获取用户名和密码
const username = document.querySelector('.username').value
const password = document.querySelector('.password').value
// console.log(username, password)
// 1.3 判断长度
if (username.length < 8) {
alertFn('用户名必须大于等于8位', false)
console.log('用户名必须大于等于8位')
return // 阻止代码继续执行
}
if (password.length < 6) {
alertFn('密码必须大于等于6位', false)
console.log('密码必须大于等于6位')
return // 阻止代码继续执行
}
// 1.4 基于axios提交用户名和密码
// console.log('提交数据到服务器')
axios({
url: 'http://hmajax.itheima.net/api/login',
method: 'POST',
data: {
username,
password
}
}).then(result => {
alertFn(result.data.message, true)
console.log(result)
console.log(result.data.message)
}).catch(error => {
alertFn(error.response.data.message, false)
console.log(error)
console.log(error.response.data.message)
})
})
</script>
</body>
</html>
1.13-form-serialize使用
![](https://img-blog.csdnimg.cn/img_convert/2720f31e6f7d345523dae4ad68bb59c7.png)
![](https://img-blog.csdnimg.cn/img_convert/49d98dd3cb7fb31719049ecedec23417.png)
![](https://img-blog.csdnimg.cn/img_convert/67bd623f29b16c8aaef348fbb8520c54.png)
![](https://img-blog.csdnimg.cn/img_convert/f4e832331af9cbef8a1083d8c69b118b.png)
![](https://img-blog.csdnimg.cn/img_convert/dee10e3c5685b6d49fee861e5247bf52.png)
![](https://img-blog.csdnimg.cn/img_convert/479b250b3f66417272845f07502f9e99.png)
![](https://img-blog.csdnimg.cn/img_convert/57cd41af12d08c4aecf47e999766ae86.png)
![](https://img-blog.csdnimg.cn/img_convert/660b90c1bfa21f573dd7e23c12d0ce6e.png)
<body>
<form action="javascript:;" class="example-form">
<input type="text" name="username">
<br>
<input type="text" name="password">
<br>
<input type="button" class="btn" value="提交">
</form>
<!--
目标:在点击提交时,使用form-serialize插件,快速收集表单元素值
1. 把插件引入到自己网页中
-->
<script src="./lib/form-serialize.js"></script>
<script>
document.querySelector('.btn').addEventListener('click', () => {
/**
* 2. 使用serialize函数,快速收集表单元素的值
* 参数1:要获取哪个表单的数据
* 表单元素设置name属性,值会作为对象的属性名
* 建议name属性的值,最好和接口文档参数名一致
* 参数2:配置对象
* hash 设置获取数据结构
* - true:JS对象(推荐)一般请求体里提交给服务器
* - false: 查询字符串
* empty 设置是否获取空值
* - true: 获取空值(推荐)数据结构和标签结构一致
* - false:不获取空值
*/
const form = document.querySelector('.example-form')
const data = serialize(form, { hash: true, empty: true })
// const data = serialize(form, { hash: false, empty: true })
// const data = serialize(form, { hash: true, empty: false })
console.log(data)
})
</script>
</body>
1.14-案例_登录_整合form-serialize
![](https://img-blog.csdnimg.cn/img_convert/5e6557f6823e439774262ed2fdfb32a6.png)
![](https://img-blog.csdnimg.cn/img_convert/36376a5e7e0370e4f1fb443199fbcdf5.png)
<!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>
<!-- 引入bootstrap.css -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css">
<!-- 公共 -->
<style>
html,
body {
background-color: #EDF0F5;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.container {
width: 520px;
height: 540px;
background-color: #fff;
padding: 60px;
box-sizing: border-box;
}
.container h3 {
font-weight: 900;
}
</style>
<!-- 表单容器和内容 -->
<style>
.form_wrap {
color: #8B929D !important;
}
.form-text {
color: #8B929D !important;
}
</style>
<!-- 提示框样式 -->
<style>
.alert {
transition: .5s;
opacity: 0;
}
.alert.show {
opacity: 1;
}
</style>
</head>
<body>
<div class="container">
<h3>欢迎-登录</h3>
<!-- 登录结果-提示框 -->
<div class="alert alert-success" role="alert">
提示消息
</div>
<!-- 表单 -->
<div class="form_wrap">
<form class="login-form">
<div class="mb-3">
<label for="username" class="form-label">账号名</label>
<input type="text" class="form-control username" name="username">
</div>
<div class="mb-3">
<label for="password" class="form-label">密码</label>
<input type="password" class="form-control password" name="password">
</div>
<button type="button" class="btn btn-primary btn-login"> 登 录 </button>
</form>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<!-- 3.1 引入插件 -->
<script src="./lib/form-serialize.js"></script>
<script>
// 目标1:点击登录时,用户名和密码长度判断,并提交数据和服务器通信
// 目标2:使用提示框,反馈提示消息
// 目标3:使用form-serialize插件,收集用户名和密码
// 2.1 获取提示框
const myAlert = document.querySelector('.alert')
/**2.2 封装提示框函数,重复调用,满足提示需求
* 功能:
* 1. 显示提示框
* 2. 不同提示文字msg,和成功绿色失败红色isSuccess(true成功,false失败)
* 3. 过2秒后,让提示框自动消失
*/
function alertFn(msg, isSuccess) {
// 1> 显示提示框
myAlert.classList.add('show')
// 2> 实现细节
myAlert.innerText = msg
const bgStyle = isSuccess ? 'alert-success' : 'alert-danger'
myAlert.classList.add(bgStyle)
// 3> 过2秒隐藏
setTimeout(() => {
myAlert.classList.remove('show')
// 提示:避免类名冲突,重置背景色
myAlert.classList.remove(bgStyle)
}, 2000)
}
// 1.1 登录-点击事件
document.querySelector('.btn-login').addEventListener('click', () => {
// 3.2 使用serialize函数,收集登录表单里用户名和密码
const form = document.querySelector('.login-form')
const data = serialize(form, { hash: true, empty: true })
console.log(data)
// {username: 'itheima007', password: '7654321'}
const { username, password } = data
// 1.2 获取用户名和密码
// const username = document.querySelector('.username').value
// const password = document.querySelector('.password').value
console.log(username, password)
// 1.3 判断长度
if (username.length < 8) {
alertFn('用户名必须大于等于8位', false)
console.log('用户名必须大于等于8位')
return // 阻止代码继续执行
}
if (password.length < 6) {
alertFn('密码必须大于等于6位', false)
console.log('密码必须大于等于6位')
return // 阻止代码继续执行
}
// 1.4 基于axios提交用户名和密码
// console.log('提交数据到服务器')
axios({
url: 'http://hmajax.itheima.net/api/login',
method: 'POST',
data: {
username,
password
}
}).then(result => {
alertFn(result.data.message, true)
console.log(result)
console.log(result.data.message)
}).catch(error => {
alertFn(error.response.data.message, false)
console.log(error)
console.log(error.response.data.message)
})
})
</script>
</body>
</html>