Vue中axios学习(一)axios中get、post请求方式

 一、axios是基于promise对ajax的一种封装

二、axios的基本使用

     1.使用默认方式发送请求,get

    <script src="../axios/axios.min.js"></script>
 
    <script>
        axios({
            //默认是get请求
            url:'http://localhost:9999/student/student/getAllSyudent'
        }).then(res>{
            console.log(res);
        })

2.指定请求方式,get,发送无参请求

    <script src="../axios/axios.min.js"></script>
    <script>
        axios({
            url:'http://localhost:9999/student/student/getAllSyudent',
            methods:'get'
        }).then(res>{
            console.log(res);
        })
    </script>

3.1 get有参请求

     <script src="../axios/axios.min.js"></script>
     <script>
         axios({
             url:'http://localhost:9999/student/student/findStudentById?id=1'
         }).then(res=>{
             console.log(res);
         })
     </script>

3.2 get有参请求其他形式

     <script src="../axios/axios.min.js"></script>
     <script>
         axios({
             url:'http://localhost:9999/student/student/findStudentById',
             params:{
                 id:'1'
             }
         }).then(res=>{
             console.log(res);
         })
     </script>

4.使用post发送无参请求

    <script src="../axios/axios.min.js"></script>
    <script>
        axios({
            url:'http://localhost:9999/student/student/pGet',
            methods:'post'
        }).then(res>{
            console.log(res);
        })
    </script>

5.使用post发送有参请求

     <script src="../axios/axios.min.js"></script>
     <script>
         axios({
             url:'http://localhost:9999/student/student/findStudentByName',
             method:'post',
             params:{
              name:'zs'
             }
         }).then(res=>{
             console.log(res);
         })
     </script>

    后台控制器接收到的name是null,axios使用post携带参数请求默认使用application/json
    解决方式一:params属性进行数据的传递
    解决方式二:"name=张娜"
    解决方式三:服务器端给接收的参数加上@requestBody

三、axios的请求方式

    1.使用axios.get方式发送无参请求

     <script src="../axios/axios.min.js"></script>
     <script>
         axios.get('http://localhost:9999/student/student/getAllStudent')
         .then(res=>{
             console.log(res);
         }).catch(err=>{
             console.log(err);
         })
     </script>

2.使用axios.get方式发送有参请求

     <script src="../axios/axios.min.js"></script>
     <script>
         axios.get('http://localhost:9999/student/student/findStudentById',
           {params:{id:1,name:'zs'}}).then(res=>{
             console.log(res);
         }).catch(err=>{
             console.log(err);
         })
     </script>

3.使用post无参请求

     <script src="../axios/axios.min.js"></script>
     <script>
         axios.post('http://localhost:9999/student/student/pGet').then(res=>{
             console.log(res);
         }).catch(err=>{
             console.log(err);
         })
     </script>

4.使用post有参请求  只修改前端代码

     <script src="../axios/axios.min.js"></script>
     <script>
     axios.post('http://localhost:9999/student/student/findStudentByName',"name=zs&age=10")
        .then(res=>{
                console.log(res);
         });
     </script>

    多个参数使用&连接

5.使用data传递数据   后台需要将axios自动转换的json数据转换为java对象  需要修改后台代码

     <script src="../axios/axios.min.js"></script>
     <script>
    axios.post('http://localhost:9999/student/student/findStudentByName',{name:'zs'})
        .then(res=>{
                console.log(res);
         });
     </script>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值