Vue:axios

本文介绍了如何在HTML页面中使用Vue.js和axios库进行HTTP请求,包括GET和POST方法,展示了如何获取和更新用户信息,以及处理查询字符串和请求头。

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

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/vue.js"></script>
    <script src="js/axios.min.js"></script>
</head>

<body>
    <div id="app">
        <button type="button" @click="f1">get</button>

        <button type="button" @click="f2">get</button>
        <button type="button" @click="f3">get</button>
        <p>用户名为:{{user.name}} ,电话号码为:{{user.phone}}</p>
    </div>
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                user: {
                    name: 'lisi',
                    phone: '12321321'
                }

            },
            methods: {
                f1: function () {
                    axios.get("http://localhost:80/test/user/1", {
                        //查询字符串
                        params: {
                            id: "25640392"
                        }
                    })
                        .then(function (res) {
                            console.log(res);
                            console.log(res.data);
                            app.user.name = res.data.data.username;
                            app.user.phone = res.data.data.mobile;
                        });

                },
                f2: function () {
                    // json
                    axios.post("http://localhost:80/test/user/1", { id: "jack" })
                        .then(function (res) {
                            console.log(res);
                        });
                },
                f3: function () {
                    axios({
                        url: "http://localhost:80/test/user/1",
                        method: "post",
                        params: {
                            //查询字符串
                            //设置请求行传值,url问号后面的参数 
                            s: "成都",
                            limit: 15
                        },
                        headers: {
                            //设置请求头
                            token: '123456'
                        },
                        data: {
                            //设置请求体(post/put)
                            //json
                            name:'jack',age:19

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




            }

        })


    </script>
</body>

</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值