1、安装json server
这并不是真的服务器
1、npm安装
npm install -g json-server
2、创建一个包含数据的json文件
{
"posts": [
{"id": 1,"title": "json-server","author": "typicode"}
],
"commets": [
{"id": 1,"body": "some comment","postId": 1}
],
"profile": {"name": "typicode"}
}
3、启动json server
在工作目录下,即db.json所在目录
json-server --watch db.json
结果:启动服务
2、axios概念
可以用axios发送ajax请求和http请求
支持promise API操作
等等
3、引入axios
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo page</title>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
<script>
console.log(axios)
</script>
</body>
</html>
4、axios的基本使用
先启动json server
json-server --watch db.json
浏览db.json的数据
http://localhost:3000/posts
axios的基本操作
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test page</title>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
<div>
<h2>axios的基本使用</h2>
<button id="send_primary">发送get请求</button>
<button id="send_post">发送post请求</button>
<button id="send_put">发送put请求</button>
<button id="send_delete">发送delet请求</button>
</div>
<script>
const send_primary=document.querySelector('#send_primary')
const send_post=document.querySelector('#send_post')
const send_put=document.querySelector('#send_put')
const send_delete=document.querySelector('#send_delete')
//发送get请求
send_primary.addEventListener('click',function () {
//发送ajax请求
axios({
method:'get',
url:'http://localhost:3000/posts/2'
}).then(response => {
console.log(response.data)
})
})
//发送post请求
send_post.addEventListener('click',function () {
console.log('put----')
//发送ajax请求
axios({
method: 'post',
url: 'http://localhost:3000/posts',
//设置请求体
data: {
title: '中国忠旺自曝严重经营困难3高管辞职',
author: '新浪网'
}
}).then(response => {
console.log(response.data)
})
})
//发送更新put请求
send_put.addEventListener('click',function () {
//发送ajax请求
axios({
method: 'put',
url: 'http://localhost:3000/posts/4',
//设置请求体
data: {
title: '苹果MacBook Pro发布 采用自研M1 Pro芯片 有刘海',
author: '环球网'
}
}).then(response => {
console.log(response.data)
})
})
//发送删除delete请求
send_delete.addEventListener('click',function () {
//发送ajax请求
axios({
method: 'delete',
url: 'http://localhost:3000/posts/4',
}).then(response => {
console.log(response.data)
})
})
</script>
</body>
</html>
5、axios的其他方式发送请求
以上是上例的数据及服务器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test page</title>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
<div>
<h2>axios的基本使用</h2>
<button id="send_primary">发送get请求</button>
<button id="send_post">发送post请求</button>
<button id="send_put">发送put请求</button>
<button id="send_delete">发送delet请求</button>
</div>
<script>
const send_primary=document.querySelector('#send_primary')
const send_post=document.querySelector('#send_post')
const send_put=document.querySelector('#send_put')
const send_delete=document.querySelector('#send_delete')
//发送get请求
send_primary.addEventListener('click',function () {
//发送ajax请求
axios.request({
method:'get',
url:'http://localhost:3000/commets'
}).then(response=>{
console.log(response)
})
})
//发送post请求
send_post.addEventListener('click',function () {
//发送ajax请求
axios.post(
//不是以对象的形式填入请求地址
'http://localhost:3000/commets',
//设置请求体,请求体是对象形式
{
body: '我是一条默不作声的评论',
postid: 2
}
).then(response => {
console.log(response)
})
})
</script>
</body>
</html>
6、axios响应结果的结构
config:配置对象
data:响应体
headers:响应头信息
request:原生ajax请求对象
status:响应状态码
statusText:响应状态字符串
7、axios配置对象
config就是配置对象
url:请求地址
method:请求方式
baseURL:请求的baseURL
transformRequest:参数序列化的配置对象
transformResponse:
headers:请求头
params:对象,请求参数,如{id:12345}
以下传参方式都是可以的
/post?a=100&b=22
/post/a/100/b/200
/post/a.100/b.22
params:{
a:100,
b:33
}
8、axios默认设置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test page</title>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
<div>
<h2>axios的基本使用</h2>
<button id="send_primary">发送get请求</button>
<button id="send_post">发送post请求</button>
<button id="send_put">发送put请求</button>
<button id="send_delete">发送delet请求</button>
</div>
<script>
const send_primary=document.querySelector('#send_primary')
const send_post=document.querySelector('#send_post')
const send_put=document.querySelector('#send_put')
const send_delete=document.querySelector('#send_delete')
//默认配置
//设置默认的请求方式
axios.defaults.method='get'
//设置baseURL
axios.defaults.baseURL='http://localhost:3000'
//设置默认参数
axios.defaults.params={i:100,a:'hello'}
//设置默认的timeout
axios.defaults.timeout=3000
//发送get请求
send_primary.addEventListener('click',function () {
//发送ajax请求
axios.request({
method:'get',
url:'/commets'
}).then(response=>{
console.log(response)
})
})
//发送post请求
send_post.addEventListener('click',function () {
//发送ajax请求
axios.post(
//不是以对象的形式填入请求地址
'http://localhost:3000/commets',
//设置请求体,请求体是对象形式
{
body: '我是一条默不作声的评论',
postid: 2
}
).then(response => {
console.log(response)
})
})
//也可以使用封装好的方法直接发送请求
send_post.addEventListener('click',function(){
//使用axios对象
axios_object.get('/getJoke').then(response=>{
console.log(response)
})
})
</script>
</body>
</html>
9、创建实例对象发送请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test page</title>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
<div>
<h2>axios的基本使用</h2>
<button id="send_primary">发送get请求</button>
<button id="send_post">发送post请求</button>
</div>
<script>
const send_primary=document.querySelector('#send_primary')
const send_post=document.querySelector('#send_post')
//发送get请求,
const axios_object=axios.create({
baseURL:'https://api.apiopen.top',
timeout:2000
})
send_primary.addEventListener('click',function(){
//使用axios对象
axios_object({
url:'/getJoke'
}).then(response=>{
console.log(response)
})
})
//也可以使用封装好的方法直接发送请求
send_post.addEventListener('click',function(){
//使用axios对象
axios_object.get('/getJoke').then(response=>{
console.log(response)
})
})
</script>
</body>
</html>
10、拦截器interceptors
一些函数,请求拦截器,响应拦截器,把关人作用
设置一个请求拦截器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test page</title>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
<div>
<h2>axios的基本使用</h2>
<button id="send_primary">发送get请求</button>
<button id="send_post">发送post请求</button>
</div>
<script>
const send_primary=document.querySelector('#send_primary')
const send_post=document.querySelector('#send_post')
axios.interceptors.request.use(function (config) {
console.log('请求拦截器成功---111')//先进后执行,先进后出
//可以修改config的参数
config.params={a:100}
return config
// throw '自己抛出的异常'
},function(error){
console.log('请求拦截器失败')
return Promise.reject(error)
})
axios.interceptors.request.use(function (config) {
console.log('请求拦截器成功---222')//就近先执行,后进先出
//可以修改config的参数
config.timeout=3000
return config
// throw '自己抛出的异常'
},function(error){
console.log('请求拦截器失败')
return Promise.reject(error)
})
axios.interceptors.response.use(function (response) {
console.log('响应拦截器成功---111')
return response
},function(error){
console.log('响应拦截器失败')
return Promise.reject(error)
})
axios.interceptors.response.use(function (response) {
console.log('响应拦截器成功----222')
return response
},function(error){
console.log('响应拦截器失败')
return Promise.reject(error)
})
axios({
method:'get',
url:'http://localhost:3000/posts'
}).then(response=>{
console.log('自定义成功的回调函数')
}).catch(reason => {
console.log('自定义失败的回调函数')
})
</script>
</body>
</html>
11、取消请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test page</title>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
</head>
<body>
<div>
<h2>axios的基本使用</h2>
<button id="send_primary">发送get请求</button>
<button id="cacel_send">取消请求</button>
</div>
<script>
const send_primary=document.querySelector('#send_primary')
const cacel_send=document.querySelector('#cacel_send')
//声明全局变量
let cancel=null
send_primary.addEventListener('click',function(){
if (cancel!== null){
cancel()
}
axios({
method:'get',
url:'http://localhost:3000/posts',
//添加配置对象返回的是一个函数
cancelToken:new axios.CancelToken(function (c) {
cancel=c//把c这个函数传给了全局变量cancel
})
}).then(respone=>{
console.log(respone)
cancel=null//如果成功了,再把全局的cancel改回去
})
})
cacel_send.addEventListener('click',function(){
cancel()
})
</script>
</body>
</html>
12、文件结构说明
dist:打包后的文件
lib:核心目录
lib/adapter:适配器,发送请求和爬虫之类相关的内容
lib/cancel:cancel取消请求,里面有cancelToken,iscancel,cancel三个文件
core:Axios.js,等等,不打了