Axios

1、Axios


1.1、什么是Axios?

Axios是专注于网络数据请求的库。相比于原生的XML HttpRequest对象,axios 更简单易用,相比于jQuery,axios 更加轻量化,只专注于网络数据请求

在服务端它使用原生 node.js http 模块,而在客户端 (浏览端) 则使用 XMLHttpRequests

1.2、Axios发送GET请求

具体语法:axios.get(url[, config])

<button type="button" class="btn btn-primary">GET请求</button>

<script>
    document.querySelector("button").addEventListener("click", () => {
        axios.get("http://localhost:8088/api/getUser", { params: { name: "晓琳", age: 22 } }).then(resp => {
            console.log(resp.data)	
        })
    })
</script>

详细配置参考官方文档

1.3、Axios发送POST请求

具体语法:axios.post(url[, data[, config]])

<button type="button" class="btn btn-info">POST请求</button>

<script>
    document.querySelector("button").addEventListener("click", () => {
        axios.post("http://localhost:8088/api/addUser", { name: "小白", age: 25 }).then(resp => {
            console.log(resp.data)
        })
    })
</script>

1.4、Axios发送请求

具体语法:`axios({

​ url: “请求URL地址”,

​ method: “请求方式”,

​ data: { /* POST数据 */ },

​ params: { /* GET参数 */ }

​ }).then(callback)`

<button type="button" class="btn btn-warning">Axios</button>

<script>
    document.querySelector("button").addEventListener("click", () => {
        axios({
            url: "http://localhost:8088/api/addUser",
            method: "POST",
            data: { 
                name: "小赵", 
                age: 23 
            },
        }).then(resp => {
            console.log(resp.data)
        })
    })
</script>

1.5、默认配置

<script>
    // 基本路径
    axios.defaults.baseURL = 'http://localhost:8088'
    // 超时时间
    axios.defaults.timeout = 3000

    document.querySelector("button").addEventListener("click", () => {
        axios({
            url: "/api/addUser",
            method: "POST",
            data: {
                address: "河南",
                location: "洛阳市"
            },
            // 请求头
            headers: { 'token': 'ASK' },
            // 文件上传处理进度事件
            onUploadProgress: function (e) {
                // console.log(e)
                if (e.event.lengthComputable) {
                    // ...
                }
            }
        }).then(resp => {
            console.log(resp.data)
        })
    })
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值