axio的使用

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

<head>
    <meta charset="UTF-8">
    <title>发送AJAX请求</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.19.2/axios.min.js"></script>
    <script>
        window.onload = function () {
            new Vue({
                el: '#app',
                data: {
                    user: {
                        name: 'xut',
                        age: 29
                    },
                    uid: '',
                    loginMsg: {}
                },
                methods: {
                    getUserById() {
                        axios.get(`https://api.github.com/users/${this.uid}`)
                            .then(resp => {
                                this.loginMsg = resp.data;
                            });
                    },
                    sendJSON() {
                        axios({
                            method: 'get',
                            url: 'user.json',
                            // data:{}
                        }).then((res) => {
                            console.log(res);
                        }).catch((resp) => {
                            console.log('请求失败' + resp.status, resp.statusText);
                        })
                    },
                    sendGet() {
                        axios.get('http://localhost:3000/info', {
                            params: {
                                name: 'song',
                                age: '10',
                                id: 1
                            }
                        }).then((res) => {
                            console.log(res);
                        }).catch((resp) => {
                            console.log('请求失败' + resp.status, resp.statusText);
                        })
                    },
                    sendPost() {
                        axios({
                            method: 'post',
                            url: 'http://localhost:3000/info',
                            data: {
                                name: 'song',
                                age: '10',
                                id: 1
                            }
                        }).then((res) => {
                            console.log(res);
                        }).catch((resp) => {
                            console.log('请求失败' + resp.status, resp.statusText);
                        })
                        // axios.post('http://localhost:3000/info',{
                        //     name:'song',
                        //     age:'10',
                        //     id:1
                        // }).then((res)=>{
                        //     console.log(res);
                        // }).catch((resp)=>{
                        //     console.log('请求失败'+resp.status,resp.statusText);
                        // })
                    },
                    sendPostFormData() {
                        //  axios.post('http://localhost:3000/info','name=alice&age=20') //方式一
                        axios.post('http://localhost:3000/info', this.user, { //transformRequest转化
                                transformRequest: [
                                    function (data) {
                                        // 对 data 进行任意转换处理
                                        // {
                                        //     name:'alice',
                                        //     age:19
                                        // }

                                        // 把json数据转化成formData
                                        var params = '';
                                        for (var index in data) {
                                            params += index + '=' + data[index] + '&'
                                        }
                                        console.log(params)
                                        return params;
                                    }
                                ]
                            })
                            .then((res) => {
                                console.log(res);
                            }).catch((resp) => {
                                console.log('请求失败' + resp.status, resp.statusText);
                            })
                        // 方式三:qs.stringify
                    }
                }
            });
        }
    </script>
</head>

<body>
    <div id="app">
        <button @click="sendJSON">发送AJAX请求</button>
        <button @click="sendGet">GET方式发送AJAX请求</button>
        <button @click="sendPost">POST方式发送AJAX请求</button>
        <button @click="sendPostFormData">POST方式发送AJAX请求 -- FormData</button>
        <hr>
        <br>
        GitHub ID: <input type="text" v-model="uid">
        <button @click="getUserById">获取指定GitHub账户信息并显示</button>
        <br>
        姓名:{{loginMsg.login}} <br>
        头像:<img :src="loginMsg.avatar_url" alt="" style="width:200px;">
    </div>
</body>

</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值