axios专题(李强)01-安装json-server,引入axios,基本使用,其他方式发送请求,响应结果的结构,配置对象,默认配置,创建实例对象发送请求,拦截器,取消请求,文件结构说明

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,等等,不打了
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值