vue之?dāng)?shù)據(jù)交互實例代碼
vue中的交互(ajax,jsonp)
vue中也存在像ajax和jsonp的數(shù)據(jù)交互,實現(xiàn)向服務(wù)器獲取數(shù)據(jù),但是他本身框架當(dāng)中沒有這樣的方法,需要一個新的小東西叫vue-resouce.js 地址:https://github.com/pagekit/vue-resource/blob/master/README.md
<meta charset="UTF-8"> <title>Document</title> <script type="text/javascript" src="vue.js"></script> <script type="text/javascript" src="vue-resource.js"></script> <style type="text/css"> </style> </head> <body> <div id="app"> <button @click="get()">按鈕</button> </div> <script type="text/javascript"> var vm=new Vue({ el:'#app', data:{ }, methods:{ get:function(){ this.$http.get('a.txt').then(function(res){ alert(res.data); },function(res){ alert(res.data); }); } } }); </script> </body>
$http 就類似于ajax 他可以this.$http.get this.$http.post還有一種就是jsonp完成跨域取數(shù)據(jù)
<div id="app"> <button @click="get()">按鈕</button> </div> <script type="text/javascript"> var vm=new Vue({ el:'#app', data:{ }, methods:{ get:function(){ this.$http.jsonp('https://sug.so.#/suggest'{word:'b'}).then(function(res){ alert(res.data.s); },function(res){ alert(res.status); }); } } }); </script> </body>
跨域取數(shù)據(jù)百度下拉例子:
<meta charset="UTF-8"> <title>Document</title> <script type="text/javascript" src="vue.js"></script> <script type="text/javascript" src="vue-resource.js"></script> <style type="text/css"> *{ margin:0; padding: 0;} .bg{width: 200px; line-height:30px;} </style> </head> <body> <div id="app"> <input type="text" v-model="t" @keyup="get($event)" @keydown.down="changeDown()" @keyup.up.prevent="changeUp()"/> <ul> <li v-for="val in arr" class="bg"> {{val}} </li> </ul> <p v-show="arr.length==0">暫無數(shù)據(jù)</p> </div> <script type="text/javascript"> var vm=new Vue({ el:'#app', data:{ arr:[], t:'', iNow:-1 }, methods:{ get:function(ev){ if(ev.keyCode==38||ev.keyCode==40){ return; } if(ev.keyCode==13){ window.open('https://www.baidu.com/s?wd='+this.t); this.t='' } this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',{wd:this.t},{jsonp:'cb'}).then(function(res){ this.arr=res.data.s },function(res){ alert('失敗'); }); }, changeDown:function(){ this.iNow++; if(this.iNow==this.arr.length)iNow=-1; this.t=this.arr[this.iNow]; }, changeUp:function(){ this.iNow--; if(this.iNow==-2)this.iNow=this.arr.length-1 this.t=this.arr[this.iNow]; } } }); </script> </body>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vite結(jié)合electron構(gòu)建前端桌面應(yīng)用程序
本文主要介紹了vite結(jié)合electron構(gòu)建前端桌面應(yīng)用程序,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Vue-cli3項目配置Vue.config.js實戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于Vue-cli3項目配置Vue.config.js的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07vue.js實現(xiàn)插入數(shù)值與表達式的方法分析
這篇文章主要介紹了vue.js實現(xiàn)插入數(shù)值與表達式的方法,結(jié)合實例形式分析了vue.js常見的3種插入數(shù)值實現(xiàn)方式,并總結(jié)了vue.js插值與表達式相關(guān)使用技巧,需要的朋友可以參考下2018-07-07vue項目打包上傳github并制作預(yù)覽鏈接(pages)
這篇文章主要介紹了vue項目打包上傳github并制作預(yù)覽鏈接(pages)的相關(guān)資料,需要的朋友可以參考下2019-04-04