axios02-其他使用方式(推荐)

本文深入解析Axios库的三种使用方式,包括GET和POST请求的具体实现,以及如何通过Promise技术处理异步操作结果。文章提供了实例代码,演示了如何在网页中使用按钮触发不同类型的HTTP请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 推荐这种方式原因: 和以前的$.ajax()非常类似

    • 不同点: 使用promise技术处理异步操作结果

    • axios({
          url:'请求路径',
          method:'请求方式',
          data:{ post请求参数 },
          params:{ get请求参数 }
      }).then(res=>{
          //成功回调
          //console.log(res)
      });
      
/*
1.学习目标介绍 : axios其他使用方式
2.学习路线 :
    (1)复习上一小节介绍的两种方式
        axios.get().then()
        axios.post().then()
    (2)介绍第三种使用方式
        axios({
            url:'请求路径',
            method:'请求方式',
            data:{ post请求参数 },
            params:{ get请求参数 }
        }).then(res=>{
            //成功回调
            //console.log(res)
        });
*/
<!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">
    <title>Document</title>
</head>

<body>
    <button id="btn1">基本使用</button>
    <button id="btn2">点我发送get请求</button>
    <button id="btn3">点我发送post请求</button>

    <!-- 导入axios -->
    <script src="./axios.js"></script>
    <script>
        /*
        1.学习目标介绍 : axios其他使用方式
        2.学习路线 :
            (1)复习上一小节介绍的两种方式
                axios.get().then()
                axios.post().then()
            (2)介绍第三种使用方式
                axios({
                    url:'请求路径',
                    method:'请求方式',
                    data:{ post请求参数 },
                    params:{ get请求参数 }
                }).then(res=>{
                    //成功回调
                    //console.log(res)
                });
        */

        //基本使用
        btn1.onclick = function () {
            axios({
                url: 'https://autumnfish.cn/api/joke',
                method: 'get',
            }).then(res => {
                console.log(res);
            })
        };

        //get请求
        btn2.onclick = function () {
            /* 
            细节: 如果get参数很少,可以使用ES6模板字符串直接在url拼接
            如果get参数很多,建议写在params中
            */
            var num = 10;
            axios({
                url: `https://autumnfish.cn/api/joke/list?num=${num}`,
                method: 'get',
                // params:{
                //     num:10
                // }
            }).then(res => {
                console.log(res);
            })
        };

        //post请求
        btn3.onclick = function () {
            axios({
                url: 'http://ttapi.research.itcast.cn/mp/v1_0/authorizations',
                method: 'post',
                data: {
                    mobile: '18801185985',
                    code: '246810'
                }
            }).then(res => {
                console.log(res);
            })
        };
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值