vue基礎(chǔ)之使用get、post、jsonp實現(xiàn)交互功能示例
本文實例講述了vue基礎(chǔ)之使用get、post、jsonp實現(xiàn)交互功能。分享給大家供大家參考,具體如下:
一、如果vue想做交互,引入: vue-resouce
二、get方式
1、get獲取一個普通文本數(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代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
希望本文所述對大家vue.js程序設(shè)計有所幫助。
- 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實現(xiàn)post文件下載
- 解決Vue axios post請求,后臺獲取不到數(shù)據(jù)的問題方法
- 解決在Vue中使用axios POST請求變成OPTIONS的問題
- vue中使用axios post上傳頭像/圖片并實時顯示到頁面的方法
- vue 2.x 中axios 封裝的get 和post方法
- Vue-cli中post請求發(fā)送Json格式數(shù)據(jù)方式
- Vue使用axios post方式將表單中的數(shù)據(jù)以json格式提交給后端接收操作實例
相關(guān)文章
vue使用$store.commit() undefined報錯的解決
這篇文章主要介紹了vue使用$store.commit() undefined報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06vue實現(xiàn)監(jiān)聽數(shù)值的變化,并捕捉到
這篇文章主要介紹了vue實現(xiàn)監(jiān)聽數(shù)值的變化,并捕捉到問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10vue實現(xiàn)excel表格的導(dǎo)入導(dǎo)出的示例
本文主要介紹了vue實現(xiàn)excel表格的導(dǎo)入導(dǎo)出的示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04解決vue項目使用font-awesome,build后路徑的問題
今天小編就為大家分享一篇解決vue項目使用font-awesome,build后路徑的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09