• vue入门到精通-Axios入门


    1.简介

    Axios 是一个开源的可以用在浏览器端和 NodeJS 的异步通信框架,她的主要作用就是实现 AJAX 异
    步通信,其功能特点如下:

    从浏览器中创建 XMLHttpRequests
    从 node.js 创建 http 请求
    支持 Promise API [ JS中链式编程 ]
    拦截请求和响应
    转换请求数据和响应数据
    取消请求
    自动转换 JSON 数据
    客户端支持防御 XSRF(跨站请求伪造)

    2. 第一个Axios

    新建json文件,模拟访问返回结果data.json

    {
    	"name": "张三",
    	"url": "https://www.baidu.com",
    	"page": 1,
    	"address": {
    		"street": "毛坦厂",
    		"city": "六安",
    		"country": "中国"
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    测试代码

    <!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>
        <script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.js"></script>
        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    </head>
    
    <body>
        <div id="app">
            {{info.name}}
        </div>
    
        <script>
            new Vue({
                el: '#app',
                data: {
                    info: {
                        name: null,
                        url: null,
                        page: null,
                        address: {
                            street: null,
                            city: null,
                            country: null
                        }
                    }
                },
                mounted() {
                    //钩子函数 
                    axios
                        .get('data.json')
                        .then(response => (this.info = response.data));
                }
            }) 
        </script>
    </body>
    
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    3.get请求

    http://wthrcdn.etouch.cn/weather_mini?city=巢湖

    <!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>第一个vue</title>
        <script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.js"></script>
        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    </head>
    
    <body>
        <div id="app">
    
        </div>
    
        <script>
            new Vue({
                el: '#app',
                data: {
    
                }
            })
    
            axios({
                method: "get",
                url: "http://wthrcdn.etouch.cn/weather_mini?city=巢湖"
            }).then(function (resp) {
                alert(JSON.stringify(resp.data));
                console.log(resp.data);
            })
    
        </script>
    </body>
    
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    或者简写方式

        axios.get("http://wthrcdn.etouch.cn/weather_mini?city=巢湖"
        ).then (function (resp) {
            alert(JSON.stringify(resp.data));
        })
    
    • 1
    • 2
    • 3
    • 4

    4.post请求

    <!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>第一个vue</title>
        <script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.js"></script>
        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    </head>
    
    <body>
        <div id="app">
    
        </div>
    
        <script>
            new Vue({
                el: '#app',
                data: {
                }
            })
    
            //2.post
            axios({
                method: "post",
                url: "https://api.apiopen.top/api/login",
                data: JSON.stringify({
                    account: "1299828500@qq.com",
                    password: "123456"
                })
            }).then(function (resp) {
                alert(JSON.stringify(resp.data));
            })
    
    
        </script>
    </body>
    
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    简化写法

            axios.post("https://api.apiopen.top/api/login", JSON.stringify({
                account: "1299828500@qq.com",
                password: "123456"
            })
            ).then(function (resp) {
                alert(JSON.stringify(resp.data));
            })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    8月HCIP新版Datacom考试通过率100%
    Mahony 滤波算法参数自动调节方法 11
    大学生网页制作教程 学生HTML静态动物网页设计作业成品 简单网页制作代码 学生宠物网页作品
    【面试】【项目】谷粒商城
    flutter系列之:flutter中的变形金刚Transform
    react环境搭建及文件配置
    Install Nginx in Linux
    【Python从入门到进阶】38、selenium关于Chrome handless的基本使用
    Android 设置定时闹铃的完整解决方案,适配到Android13
    Vue Slot插槽:组件化的艺术
  • 原文地址:https://blog.csdn.net/weixin_44480609/article/details/125509665