axios简单使用

axios简单使用

<!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>axios基本使用</title>
</head>
<body>
    <div id="app">
        <input type="button" value="获取笑话" @click="getJoke"/>
        <p>{{ joke }}</p>
    </div>
    <input type="button" value="get请求" class="get">
    <input type="button" value="post请求" class="post">
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="vue.js"></script>
    <script>
        //随机笑话
        document.querySelector(".get").onclick = function() {
            axios.get("https://autumnfish.cn/api/joke/list?num=6")
                .then(function(res) {
                    console.log(res);
                }, function(err){
                    console.log(err);
                })
        }

        //页面加载时请求,如果调用某个方法返回值是Promise实例,则前面可以添加await
        //await只能用在被async修饰的方法中
        // axios({
        //     method: 'GET',
        //     url: 'https://autumnfish.cn/api/joke/list',
        //     //URL中的查询参数
        //     params: {
        //         num:6
        //     },
        //     //请求体参数
        //     data: {

        //     }
        // }).then(function(res){
        //     console.log(res);
        // })

        //解构赋值时使用冒号进行重命名
        // const { data:resp } = await axios({
        //     method: 'GET',
        //     url: 'https://autumnfish.cn/api/joke/list',
        //     //URL中的查询参数
        //     params: {
        //         num:6
        //     },

        // })

        //用户注册
        document.querySelector(".post").onclick = function() {
            axios.post('https://autumnfish.cn/api/user/reg', {username: "jack"})
                .then(function(res){
                    console.log(res);
                }, function(err){
                    console.log(err);
                });
        }

        var app = new Vue({
            el: '#app',
            data: {
                joke: ""
            },
            methods: {
                getJoke: function() {
                    let that = this;
                    axios.get("https://autumnfish.cn/api/joke").then(function(res){
                        that.joke = res.data;
                    }, function(err){})
                }
            }
        })
    </script>
</body>
</html>

image-20230623205843582

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐江小鱼

知识创造财富,余额还是小数

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值