1. 程式人生 > >Vue之交互

Vue之交互

sta resource title then jsonp url viewport www ack

1.get()

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable"
content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script src="vue-resource.js"></script> <script> window.onload=function
(){ new Vue({ el:body, data:{ }, methods:{ get:function(){ this.$http.get(a.txt).then(function(res){ alert(res.data); },
function(res){ alert(res.data); }); } } }); }; </script> </head> <body> <input type="button" value="按鈕" @click="get()"> </body> </html>

描述:

使用this.$http.get(‘數據來源的文件‘).then(function(res){

//成功

alert(res.data)//文件中的數據

},function(res){

//失敗
alert(res.status);//返回狀態:0

})

2.使用帶參數的數據文件

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable" content="yes">  
    <meta name="apple-mobile-web-app-status-bar-style" content="black">  
    <style>  
  
    </style>  
    <script src="vue.js"></script>  
    <script src="vue-resource.js"></script>  
    <script>  
        window.onload=function(){  
            new Vue({  
                el:body,  
                data:{  
  
                },  
                methods:{  
                    get:function(){  
                        this.$http.get(get.php,{  
                            a:1,  
                            b:2  
                        }).then(function(res){  
                            alert(res.data);  
                        },function(res){  
                            alert(res.status);  
                        });  
                    }  
                }  
            });  
        };  
    </script>  
</head>  
<body>  
    <input type="button" value="按鈕" @click="get()">  
</body>  
</html> 

get.php文件中

<?php  
$a=$_GET[‘a‘];  
$b=$_GET[‘b‘];  
echo $a+$b;  
?> 

結果:

輸出 3

3.使用post(帶參數)

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable" content="yes">  
    <meta name="apple-mobile-web-app-status-bar-style" content="black">  
    <style>  
  
    </style>  
    <script src="vue.js"></script>  
    <script src="vue-resource.js"></script>  
    <script>  
        window.onload=function(){  
            new Vue({  
                el:body,  
                data:{  
  
                },  
                methods:{  
                    get:function(){  
                        this.$http.post(post.php,{  
                            a:1,  
                            b:20  
                        },{  
                            emulateJSON:true  
                        }).then(function(res){  
                            alert(res.data);  
                        },function(res){  
                            alert(res.status);  
                        });  
                    }  
                }  
            });  
        };  
    </script>  
</head>  
<body>  
    <input type="button" value="按鈕" @click="get()">  
</body>  
</html>  

描述:

emulateJSON:true

如果Web服務器無法處理編碼為application/json的請求,你可以啟用emulateJSON選項。

啟用該選項後,請求會以application/x-www-form-urlencoded作為MIME type,就像普通的HTML表單一樣。

4.使用jsonp

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable" content="yes">  
    <meta name="apple-mobile-web-app-status-bar-style" content="black">  
    <style>  
  
    </style>  
    <script src="vue.js"></script>  
    <script src="vue-resource.js"></script>  
    <script>  
        window.onload=function(){  
            new Vue({  
                el:body,  
                data:{  
  
                },  
                methods:{  
                    get:function(){  
                        this.$http.jsonp(https://sug.so.360.cn/suggest,{  
                            word:a  
                        }).then(function(res){  
                            alert(res.data.s);  
                        },function(res){  
                            alert(res.status);  
                        });  
                    }  
                }  
            });  
        };  
    </script>  
</head>  
<body>  
    <input type="button" value="按鈕" @click="get()">  
</body>  
</html> 

Vue之交互