vue教程1-08 互動 get、post、jsonp

來源:互聯網
上載者:User

標籤:oct   lan   tps   title   images   doctype   .com   教程   str   

vue教程1-08 互動 get、post、jsonp

一、如果vue想做互動,引入: vue-resouce

二、get方式

1、get擷取一個普通文本資料:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title>    <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.status);//成功                            alert(res.data);                        },function(res){                            alert(res.status);//失敗返回                            alert(res.data);                        });                    }                }            });        };    </script></head><body>    <input type="button" value="按鈕" @click="get()"></body></html>

2、get給服務發送資料:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title>    <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>

二、post方式

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title>    <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>

四、jsonp方式

擷取百度介面

 

查看響應資料

 

jsonp請求百度介面

 

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title>    <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://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su‘,{                            wd:‘a‘                        },{                            jsonp:‘cb‘//回呼函數名稱                        }).then(function(res){                            alert(res.data.s);                        },function(res){                            alert(res.status);                        });                    }                }            });        };    </script></head><body>    <input type="button" value="按鈕" @click="get()"></body></html>

jsonp請求360介面

 

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title>    <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教程1-08 互動 get、post、jsonp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.