Vue2學(xué)習(xí)筆記之請求數(shù)據(jù)交互vue-resource
基本語法
必須引入一個(gè)庫:vue-resource github地址
// 基于全局Vue對象使用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的請求API是按照REST風(fēng)格設(shè)計(jì)的,它提供了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 | 單位為毫秒的請求超時(shí)時(shí)間 (0 表示無超時(shí)時(shí)間) |
| before | function(request) | 請求發(fā)送前的處理函數(shù),類似于jQuery的beforeSend函數(shù) |
| progress | function(event) | ProgressEvent回調(diào)處理函數(shù) |
| credentials | boolean | 表示跨域請求時(shí)是否需要使用憑證 |
| emulateHTTP | boolean | 發(fā)送PUT, PATCH, DELETE請求時(shí)以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)備一個(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請求
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>
上面代碼實(shí)現(xiàn)了,點(diǎn)擊按鈕,就發(fā)送get請求,成功就會(huì)執(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實(shí)現(xiàn)請求數(shù)據(jù)的方法示例
- vuejs前后端數(shù)據(jù)交互之從后端請求數(shù)據(jù)的實(shí)例
- vue請求數(shù)據(jù)的三種方式
- vue 請求后臺數(shù)據(jù)的實(shí)例代碼
- vue中promise的使用及異步請求數(shù)據(jù)的方法
- vue中實(shí)現(xiàn)先請求數(shù)據(jù)再渲染dom分享
- 談一談vue請求數(shù)據(jù)放在created好還是mounted里好
- vue2實(shí)現(xiàn)數(shù)據(jù)請求顯示loading圖
- Vue.js+HighCharts實(shí)現(xiàn)動(dòng)態(tài)請求展示時(shí)序數(shù)據(jù)
相關(guān)文章
一次前端Vue項(xiàng)目國際化解決方案的實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于前端Vue項(xiàng)目國際化解決方案的實(shí)戰(zhàn)記錄,以上只是一部分Vue項(xiàng)目開發(fā)中遇到的典型問題和解決方案,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07
Vue通過echarts實(shí)現(xiàn)數(shù)據(jù)圖表化顯示
Echarts,它是一個(gè)與框架無關(guān)的 JS 圖表庫,但是它基于Js,這樣很多框架都能使用它,例如Vue,估計(jì)IONIC也能用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
VUE實(shí)現(xiàn)token登錄驗(yàn)證
這篇文章主要為大家介紹了VUE實(shí)現(xiàn)token登錄驗(yàn)證,詳細(xì)記錄實(shí)現(xiàn)token登錄驗(yàn)證的步驟,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
前端vue+express實(shí)現(xiàn)文件的上傳下載示例
本文主要介紹了前端vue+express實(shí)現(xiàn)文件的上傳下載示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Vue報(bào)錯(cuò)"Failed?to?resolve?loader:less-loader"的解決方
這篇文章主要給大家介紹了關(guān)于Vue報(bào)錯(cuò)"Failed?to?resolve?loader:less-loader"的解決方法,文中通過圖文介紹的非常詳細(xì),對同樣遇到這樣問題的朋友具有一定的需要的朋友可以參考下2023-02-02

