zoukankan      html  css  js  c++  java
  • Vue之交互

    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> 
  • 相关阅读:
    效果超酷的textarea的输入字数限提示
    【设计模式(七)】结构型模式之桥接模式
    【设计模式(六)】适配器模式
    【设计模式(四)】原型模式
    【设计模式(三)】工厂模式
    【设计模式(二)】单例模式
    【设计模式(一)】设计模式概览与六大设计原则
    【算法刷题】无重复字符的最长子串
    【算法刷题】全排列 II
    【算法刷题】LRU缓存模拟
  • 原文地址:https://www.cnblogs.com/chaofei/p/7706828.html
Copyright © 2011-2022 走看看