Vue2學(xué)習(xí)筆記之請求數(shù)據(jù)交互vue-resource
基本語法
必須引入一個庫:vue-resource github地址
// 基于全局Vue對象使用http Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback); Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback); // 在一個Vue實例內(nèi)使用$http this.$http.get('/someUrl', [options]).then(successCallback, errorCallback); this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
vue-resource的請求API是按照REST風(fēng)格設(shè)計的,它提供了7種請求API:
get(url, [options])
head(url, [options])
delete(url, [options])
jsonp(url, [options])
post(url, [body], [options])
put(url, [body], [options])
patch(url, [body], [options])
Options
Parameter | Type | Description |
---|---|---|
url | string | 請求的UR |
body | Object, FormData, string | request body |
headers | Object | request header |
params | Object | 請求的URL參數(shù)對象 |
method | string | 請求的HTTP方法,例如:'GET', 'POST'或其他HTTP方法 |
timeout | number | 單位為毫秒的請求超時時間 (0 表示無超時時間) |
before | function(request) | 請求發(fā)送前的處理函數(shù),類似于jQuery的beforeSend函數(shù) |
progress | function(event) | ProgressEvent回調(diào)處理函數(shù) |
credentials | boolean | 表示跨域請求時是否需要使用憑證 |
emulateHTTP | boolean | 發(fā)送PUT, PATCH, DELETE請求時以HTTP POST的方式發(fā)送,并設(shè)置請求頭的X-HTTP-Method-Override |
emulateJSON | boolean | 將request body以application/x-www-form-urlencoded content type發(fā)送 |
1. 向文本發(fā)出get請求
準(zhǔn)備一個1.txt 的文本數(shù)據(jù),時面的內(nèi)容是:welcomet to vue!!!
<!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> <script src="http://unpkg.com/vue/dist/vue.js"></script> <script src="http://files.cnblogs.com/files/zycbloger/vue-resource.min.js"></script> <script type="text/javascript"> window.onload = function(){ var vm = new Vue({ el:'#box', data:{ msg:'Hello World!', }, methods:{ get:function(){ //發(fā)送get請求 this.$http.get('1.txt').then(function(res){ alert(res.body); },function(){ alert('請求失敗處理'); //失敗處理 }); } } }); } </script> </head> <body> <div id="box"> <input type="button" @click="get()" value="按鈕"> </div> </body> </html>
上面代碼實現(xiàn)了,點擊按鈕,就發(fā)送get請求,成功就會執(zhí)行彈窗 welcomet to vue!!!
2. 關(guān)于向后端請求,并帶參數(shù)的寫法
<!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8"> <script src="http://unpkg.com/vue/dist/vue.js"></script> <script src="http://files.cnblogs.com/files/zycbloger/vue-resource.min.js"></script> <script type="text/javascript"> window.onload = function(){ var vm = new Vue({ el:'#box', data:{ msg:'Hello World!', }, methods:{ get:function(){ //發(fā)送get請求 this.$http.get('get.do',{a:1,b:2}).then(function(res){ alert(res.body); },function(){ alert('請求失敗處理'); //失敗處理 }); }, post:function(){ //發(fā)送post請求 this.$http.post('post.do',{a:1,b:2}).then(function(res){ alert(res.body); },function(){ alert('請求失敗處理'); //失敗處理 }); } } }); } </script> </head> <body> <div id="box"> <input type="button" @click="get()" value="按鈕get"> <input type="button" @click="post()" value="按鈕post"> </div> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue如何從接口請求數(shù)據(jù)
- vue.js實現(xiàn)請求數(shù)據(jù)的方法示例
- vuejs前后端數(shù)據(jù)交互之從后端請求數(shù)據(jù)的實例
- vue請求數(shù)據(jù)的三種方式
- vue 請求后臺數(shù)據(jù)的實例代碼
- vue中promise的使用及異步請求數(shù)據(jù)的方法
- vue中實現(xiàn)先請求數(shù)據(jù)再渲染dom分享
- 談一談vue請求數(shù)據(jù)放在created好還是mounted里好
- vue2實現(xiàn)數(shù)據(jù)請求顯示loading圖
- Vue.js+HighCharts實現(xiàn)動態(tài)請求展示時序數(shù)據(jù)
相關(guān)文章
Vue中 v-if 和v-else-if頁面加載出現(xiàn)閃現(xiàn)的問題及解決方法
vue中v-if 和v-else-if在頁面加載的時候,不滿足條件的標(biāo)簽會加載然后再消失掉,如果要解決這個問題,下面小編給大家?guī)砹藢嵗a,需要的朋友參考下吧2018-10-10vue3.0+vue-router+element-plus初實踐
這篇文章主要介紹了vue3.0+vue-router+element-plus初實踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12前端實現(xiàn)pdf預(yù)覽功能的全過程(基于vue)
這篇文章主要給大家介紹了關(guān)于前端實現(xiàn)pdf預(yù)覽功能的相關(guān)資料,前端實現(xiàn)預(yù)覽最好的效果還是PDF,不會出現(xiàn)一些文字錯亂和亂碼的問題,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09一款移動優(yōu)先的Solid.js路由solid router stack使用詳解
這篇文章主要為大家介紹了一款移動優(yōu)先的Solid.js路由solid router stack使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08