get post jsonp三種數(shù)據(jù)交互形式實例詳解
一、get請求
1.引入 vue.js 和 vue-resource.js , 準備一個按鈕
<input type="button" value="按鈕" @click="get()"/> //點擊按鈕請求數(shù)據(jù)函數(shù)get()
2.準備一個txt文件
welcome vue
3.編寫js代碼
<script>
window.onload=function(){
new Vue({
el:'body', //主體為body,有套div時,此處為選擇器
methods:{
get:function(){
this.$http.get('a.txt').then(function(res){
alert(res.data) //成功后,彈出請求數(shù)據(jù)
},function(res){
alert(res.status) //失敗后,彈出請求狀態(tài)碼
})
}
}
})
}
</script>
二、post請求
1.引入 vue.js 和 vue-resource.js , 準備一個按鈕
<input type="button" value="按鈕" @click="get()"/>
2.準備一個php文件
<?php $a=$_POST['a']; $b=$_POST['b']; echo $a-$b; //回顯數(shù)據(jù)相減結(jié)果 ?>
3.編寫js代碼
<script>
window.onload=function(){
new Vue({
el:'body',
methods:{
get:function(){
this.$http.post('post.php',{ //發(fā)送實參數(shù)據(jù),進行運算(需要放在服務器環(huán)境)
a:1,
b:2
},{
emulateJSON:true //post的標識
}).then(function(res){
alert(res.data) //成功后彈出數(shù)據(jù)結(jié)果
},function(res){
alert(res.status) //失敗后彈出狀態(tài)碼
})
}
}
})
}
</script>
三、jsonp——百度下拉列表實例
1.引入 vue.js 和 vue-resource.js , 準備基礎(chǔ)樣式代碼
<style>
.gray{
background: #ccc; //按上下鍵時顯示的文字背景顏色
}
</style>
<div id="box">
<input type="text" v-model="t1" @keyup="get($event)" @keydown.down="changeDown()" @keydown.up.prevent="changeUp()"/>
//按鍵傳鍵值 get($event) 函數(shù) //按向下鍵時 changeDown() 函數(shù) //按向上鍵時 changeUp() 函數(shù):阻止默認行為輸入浮上移
<ul>
<li v-for="value in myData" :class="{gray:$index==now}">{{value}}</li>
//循環(huán)myData數(shù)據(jù) 綁定樣式同時添加條件,下標值此時為幾時,背景為灰
</ul>
<p v-show="myData.length==0">暫無數(shù)據(jù)...</p> //當數(shù)據(jù)長度為0時,顯示暫無數(shù)據(jù)...
</div>
2、編寫js代碼
<script>
window.onload=function(){
new Vue({
el:'#box',
data:{
myData:[],
t1:'',
now:-1
},
methods:{
get:function(ev){ //接收事件
if(ev.keyCode==38||ev.keyCode==40)return; //如果事件為向上向下則return不請求數(shù)據(jù)
if(ev.keyCode==13){ //如果事件為回車
window.open('https://www.baidu.com/s?wd='+this.t1); //則打開百度對應t1值頁面
this.t1=''; //清空輸入框
}
this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',{
wd:this.t1 //截取的搜索接口,發(fā)送數(shù)據(jù)為輸入框此時輸入的數(shù)據(jù)
},{
jsonp:'cb' //callback名字,默認為'callback'
}).then(function(res){
this.myData=res.data.s //將數(shù)據(jù)的s值賦給 myData
},function(res){
alert(res.status)
})
},
changeDown:function(){ //按下鍵時的函數(shù)
this.now++; //now下標值++
if(this.now==this.myData.length)this.now=-1; //如果下標值為數(shù)據(jù)長度,即最后一個時,為-1,跳到第一個
this.t1=this.myData[this.now] //輸入框值為此時數(shù)據(jù)中選中的值
},
changeUp:function(){ //按上鍵時的函數(shù)
this.now--; //now下標值--
if(this.now==-2)this.now=this.myData.length-1 //如果下標值為-2,此時now=總長度-1,跳到最后一個
this.t1=this.myData[this.now] //輸入框值為此時數(shù)據(jù)中選中的值
}
}
})
}
</script>
3、類似百度搜索了。。。
總結(jié)
以上所述是小編給大家介紹的三種數(shù)據(jù)交互形式get post jsonp實例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 基于ajax和jsonp的原生封裝(實例)
- 原生js的ajax和解決跨域的jsonp(實例講解)
- 基于js原生和ajax的get和post方法以及jsonp的原生寫法實例
- 全面解析Ajax和jsonp使用總結(jié)
- 使用原生js封裝的ajax實例(兼容jsonp)
- 利用jsonp與代理服務器方案解決跨域問題
- Angular2 http jsonp的實例詳解
- 用nodejs實現(xiàn)json和jsonp服務的方法
- 使用jquery的jsonp如何發(fā)起跨域請求及其原理詳解
- 詳解java 中Spring jsonp 跨域請求的實例
- Vue2.0 vue-source jsonp 跨域請求
- 原生js jquery ajax請求以及jsonp的調(diào)用方法
- 深入講解xhr(XMLHttpRequest)/jsonp請求之a(chǎn)bort
- AngularJS實現(xiàn)的JSONP跨域訪問數(shù)據(jù)傳輸功能詳解
- jsonp跨域請求詳解
- 關(guān)于jQuery.ajax()的jsonp碰上post詳解
- jQuery Jsonp跨域模擬搜索引擎
- 詳細分析jsonp的原理和實現(xiàn)方式
相關(guān)文章
url傳遞的參數(shù)值中包含&時,url自動截斷問題的解決方法
下面小編就為大家?guī)硪黄猽rl傳遞的參數(shù)值中包含&時,url自動截斷問題的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
Underscore之Array_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了Underscore之Array的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
javascript css styleFloat和cssFloat
在寫js操作css的過程中發(fā)現(xiàn)float屬性在IE和firefox下對應的js腳本是不一樣的,IE下對應得是 styleFloat,firefox,chorme,safari下對應的是cssFloat,可用in運算符去檢測style是否包含此屬性。2010-03-03
Bootstrap3使用typeahead插件實現(xiàn)自動補全功能
這篇文章主要介紹了Bootstrap3使用typeahead插件實現(xiàn)自動補全功能的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07

