vue請求方式axios和axios.get

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <title>axios</title>
        </head>
        <script type="text/javascript" src="js/vue.js" ></script>
        <script type="text/javascript" src="js/axios.min.js" ></script>
        <script type="text/javascript" src="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;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章