vue基礎(chǔ)之使用get、post、jsonp實(shí)現(xiàn)交互功能示例
本文實(shí)例講述了vue基礎(chǔ)之使用get、post、jsonp實(shí)現(xiàn)交互功能。分享給大家供大家參考,具體如下:
一、如果vue想做交互,引入: vue-resouce
二、get方式
1、get獲取一個(gè)普通文本數(shù)據(jù):
<!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給服務(wù)發(fā)送數(shù)據(jù):
<!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方式
獲取百度接口
查看響應(yīng)數(shù)據(jù)
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'//回調(diào)函數(shù)名稱 }).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.#/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>
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運(yùn)行效果。
希望本文所述對大家vue.js程序設(shè)計(jì)有所幫助。
- vue中axios處理http發(fā)送請求的示例(Post和get)
- 詳解vue axios用post提交的數(shù)據(jù)格式
- 詳解Vue用axios發(fā)送post請求自動set cookie
- 解決vue處理axios post請求傳參的問題
- vue axios post發(fā)送復(fù)雜對象問題
- vue+axios實(shí)現(xiàn)post文件下載
- 解決Vue axios post請求,后臺獲取不到數(shù)據(jù)的問題方法
- 解決在Vue中使用axios POST請求變成OPTIONS的問題
- vue中使用axios post上傳頭像/圖片并實(shí)時(shí)顯示到頁面的方法
- vue 2.x 中axios 封裝的get 和post方法
- Vue-cli中post請求發(fā)送Json格式數(shù)據(jù)方式
- Vue使用axios post方式將表單中的數(shù)據(jù)以json格式提交給后端接收操作實(shí)例
相關(guān)文章
vue使用$store.commit() undefined報(bào)錯(cuò)的解決
這篇文章主要介紹了vue使用$store.commit() undefined報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Vuepress使用vue組件實(shí)現(xiàn)頁面改造
這篇文章主要為大家介紹了Vuepress使用vue組件實(shí)現(xiàn)頁面改造示例過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07vue實(shí)現(xiàn)監(jiān)聽數(shù)值的變化,并捕捉到
這篇文章主要介紹了vue實(shí)現(xiàn)監(jiān)聽數(shù)值的變化,并捕捉到問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10vue實(shí)現(xiàn)excel表格的導(dǎo)入導(dǎo)出的示例
本文主要介紹了vue實(shí)現(xiàn)excel表格的導(dǎo)入導(dǎo)出的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Vue3實(shí)現(xiàn)登錄表單驗(yàn)證功能
這篇文章主要介紹了Vue3實(shí)現(xiàn)登錄表單驗(yàn)證功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06解決vue項(xiàng)目使用font-awesome,build后路徑的問題
今天小編就為大家分享一篇解決vue項(xiàng)目使用font-awesome,build后路徑的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09詳解如何實(shí)現(xiàn)一個(gè)簡單的 vuex
本篇文章主要介紹了如何實(shí)現(xiàn)一個(gè)簡單的 vuex,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02