为了账号安全,请及时绑定邮箱和手机立即绑定

vue请求方式axios和axios.get

标签:
Vue.js

         

    <!DOCTYPE html>    <html>        <head>            <meta charset="utf-8" />            <title>axios</title>        </head>        <script type="text/javascript" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="js/vue.js" ></script>        <script type="text/javascript" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="js/axios.min.js" ></script>        <script type="text/javascript" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="js/vue-resource.min.js" ></script>        <script>            window.onload = function(){                //配置是否允许检查代码,方便调试,生产环境中设置为false                Vue.config.devtools = true;  //检查代码                Vue.config.productioinTip = false;  //有强迫症的,可以关掉生产中的提示信息                new Vue({                    el:'#div1',                    data:{                        user:{                            name: 'lili',                            age:20                        }                    },                    methods:{                        send(){                            axios({                                method:'get',                                url:'user.json'                            }).then(function(res){                                console.log(res.data);                            }).catch(function(msg){                                console.log(msg.status);                            });                        },                        getSend(){                            axios.get('user.php',{                                params:{                                    name: '李四',                                    age: 19                                }                            }).then(res => {                                console.log(res.data);                            }).catch(err => {                                console.log('get请求失败:'+err.status+','+err.statusText);                            })                        },                        postSend(){                            axios.post('server.php',this.user,{                                transformRequest:[                                    function(data){                                        let params = '';                                        for(let index in data){                                            params += index+'='+data[index]+'&';                                        }                                        return params;                                    }                                ]                            }).then(resp => {                                console.log(resp.data);                            }).catch(err => {                                console.log('请求失败:'+err.status+','+err.statusText);                            })                        },                        jsonpSend(){                            this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',{                                params:{                                    wd:'a' //百度的参数                                },                                jsonp: 'cb',//可以修改对方请求参数名称                            }).then(res => {                                console.log(res.data);                            })                        }                    }                })            }        </script>        <body>            <div id="div1">                <button @click="send">axios请求</button>                <button @click="getSend">get请求</button>                <button @click="postSend">post请求</button>                <button @click="jsonpSend">jsonp请求</button>            </div>        </body>    </html>
<?php    $name = $_GET['name'];    $age = $_GET['age'];    $msg =  '用户名:'.$name.',年龄:'.$age;    echo $msg;
<?php    $name = $_POST['name'];    $age = $_POST['age'];    $msg =  '用户名:'.$name.',年龄:'.$age;    echo $msg;


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 1
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消