axios的基本使用

//使用默认方式发送无参请求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="./javascript/axios.min.js"></script>
    <title>Document</title>
</head>
<body>
<script>
    axios({//默认get请求
        url:'https://ai.taobao.com/',
    }).then(res=>{
        console.log(res);
    });
</script>
</body>
</html>

 

//axios发送get方式的有参请求【方式一】

<script>
    axios({
        url:'http://localhost:8080/student/getStudentById?id=1',
    }).then(res=>{
        console.log(res);
    });
</script>

  

//axios发送get方式的有参请求【方式二】

<script>
    axios({
        url:'http://localhost:8080/student/getStudentById',
        params:{
            id:'1',
            name:'zhangsan'
        }
    }).then(res=>{
        console.log(res);
    });
</script>

  

 

//使用post方式发送无参请求
<script>
    axios({
        url:'http://localhost:8080/student/getAllStudent',
        method:'post'
    }).then(res=>{
        console.log(res);
    })
</script>

  

//使用post方式发送有参请求【默认格式application/json】方式一

<script>
    axios({
        url:'http://localhost:8080/student/getStudentById',
        method:'post',
        data:{//后端接收为null,需要添加@RequestBody注解
            id:1,
            name:'zhangsan'
        },      
        headers: {'Content-Type': 'application/json'}
    }).then(res=>{
        console.log(res);
    });
</script>

  

1
2
3
4
5
6
7
8
9
10
11
<script>
    let formData = JSON.stringify(this.dataObj); //将vue实例对象转换成json字符串
    axios({
        url:'http://localhost:8080/student/getStudentById',
        method:'post',
        data:formData ,
        headers: {'Content-Type': 'application/json'}
    }).then(res=>{
        console.log(res);
    });
</script>

  

 

 

方式二

<script>
    axios({
        url:'http://localhost:8080/student/getStudentById',
        method:'post',
        params:{//url上拼接参数,后端可以接受参数
            id:1,
            name:'zhangsan'
        }
    }).then(res=>{
        console.log(res);
    });
</script>

  

posted @   iTao0128  阅读(414)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示