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