vue處理get/post的http請(qǐng)求的實(shí)例
一、使用Vue.http/this.$http
在發(fā)起請(qǐng)求的時(shí)候,為了減少作用域鏈的搜索,建議使用一個(gè)局部變量來(lái)接受this
1. GET請(qǐng)求
// 基于全局Vue對(duì)象使用http
Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);
// 在一個(gè)Vue實(shí)例內(nèi)使用$http
this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);
示例
//不帶參數(shù)的get請(qǐng)求
this.$http.get('/someUrl').then(function(res){
console.log('請(qǐng)求成功處理');
},function(res){
console.log('請(qǐng)求失敗處理');
});
//需要傳遞數(shù)據(jù)的get請(qǐng)求
this.$http.get('/someUrl',{param:jsonData}).then(function(res){
console.log('請(qǐng)求成功處理');
},function(res){
console.log('請(qǐng)求失敗處理');
});
//ES6的Lambda寫(xiě)法
this.$http.get('/someUrl', [options]).then((response) => {
console.log('請(qǐng)求成功處理');
}, (response) => {
console.log('請(qǐng)求失敗處理');
});
2.POST請(qǐng)求
post 發(fā)送數(shù)據(jù)到后端,需要第三個(gè)參數(shù) {emulateJSON:true}。
emulateJSON 的作用: 如果Web服務(wù)器無(wú)法處理編碼為 application/json 的請(qǐng)求,你可以啟用 emulateJSON 選項(xiàng)。啟用該選項(xiàng)后,請(qǐng)求會(huì)以application/x-www-form-urlencoded作為MIME type,就像普通的HTML表單一樣。
// 基于全局Vue對(duì)象使用http
Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
// 在一個(gè)Vue實(shí)例內(nèi)使用$http
this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
示例
this.$http.post('/try/ajax/demo_test_post.php',{name:"菜鳥(niǎo)教程",url:"http://www.runoob.com"},{emulateJSON:true}).then(function(res){
document.write(res.body);
},function(res){
console.log(res.status);
});
二、使用Vue.resource/this.$resource
vue-resource提供了另外一種方式訪問(wèn)HTTP——resource服務(wù),resource服務(wù)包含以下幾種默認(rèn)的action:
get: {method: 'GET'},
save: {method: 'POST'},
query: {method: 'GET'},
update: {method: 'PUT'},
remove: {method: 'DELETE'},
delete: {method: 'DELETE'}
訪問(wèn)resource對(duì)象的兩種方式:
//全局訪問(wèn) Vue.resource //實(shí)例訪問(wèn) this.$resource
GET請(qǐng)求
//使用一個(gè)局部變量來(lái)接受this
var vm = this;
this.$resource('apiUrl').get().then((response) => {
console.log("調(diào)用成功");
})
.catch(function(response) {
console.log("調(diào)用失敗");
})
}
POST請(qǐng)求
使用save方法發(fā)送POST請(qǐng)求
//使用一個(gè)局部變量來(lái)接受this
var vm = this;
this.$resource('apiUrl').save('apiUrl',Target).then((response) => {
console.log("調(diào)用成功");
})
.catch(function(response) {
console.log("調(diào)用失敗");
})
}
inteceptor – 在請(qǐng)求前和請(qǐng)求后附加行為
攔截器可以在請(qǐng)求發(fā)送前和發(fā)送請(qǐng)求后做一些處理

用法
//Lambda函數(shù)寫(xiě)法
Vue.http.interceptors.push((request, next) => {
// ...
// 請(qǐng)求發(fā)送前的處理邏輯
// ...
next((response) => {
// ...
// 請(qǐng)求發(fā)送后的處理邏輯
// ...
// 根據(jù)請(qǐng)求的狀態(tài),response參數(shù)會(huì)返回給successCallback或errorCallback
return response
})
})
//普通寫(xiě)法
Vue.http.interceptors.push(function(request, next) {
// ...
// 請(qǐng)求發(fā)送前的處理邏輯
// ...
next(function(response) {
// ...
// 請(qǐng)求發(fā)送后的處理邏輯
// ...
// 根據(jù)請(qǐng)求的狀態(tài),response參數(shù)會(huì)返回給successCallback或errorCallback
return response
})
})
實(shí)例 – 為所有的請(qǐng)求處理加一個(gè)loading
請(qǐng)求發(fā)送前顯示loading,接收響應(yīng)后隱藏loading或顯示指定的loading信息;
添加loading.vue組件
<template id="loading-template"> <div class="loading-overlay"> <div class="sk-three-bounce"> <div class="sk-child sk-bounce1"></div> <div class="sk-child sk-bounce2"></div> <div class="sk-child sk-bounce3"></div> </div> </div> </template>
在父組件中引入loading組件
<template>
<div class="father">
//loading 發(fā)起請(qǐng)求時(shí)顯示
<loading v-show="showLoading"</loading>
//modal-dialog 請(qǐng)求失敗時(shí)顯示
<modal-dialog :show="showDialog">
<header class="dialog-header" slot="header">
<h1 class="dialog-title">Server Error</h1>
</header>
<div class="dialog-body" slot="body">
<p class="error">Oops,server has got some errors, error code: {{errorCode}}.</p>
</div>
</modal-dialog>
</div>
</template>
在父組件中添加inteceptor
data: {
showLoading: false,
showDialog: false,
errorCode: ''
},
//在生命周期中添加inteceptor
Vue.http.interceptors.push((request, next) => {
help.showLoading = true
next((response) => {
if(!response.ok){
help.errorCode = response.status
help.showDialog = true
}
help.showLoading = false
return response
});
});
拓展
vue-resource 提供了 7 種請(qǐng)求 API(REST 風(fēng)格):
get(url, [options]) head(url, [options]) delete(url, [options]) jsonp(url, [options]) post(url, [body], [options]) put(url, [body], [options]) patch(url, [body], [options])
除了 jsonp 以外,另外 6 種的 API 名稱(chēng)是標(biāo)準(zhǔn)的 HTTP 方法。其中,options參數(shù)說(shuō)明如下:

可以通過(guò)如下屬性和方法處理一個(gè)請(qǐng)求獲取到的響應(yīng)對(duì)象:

參考文章
代碼是參考上面兩篇文章寫(xiě)出來(lái)的,沒(méi)有實(shí)際運(yùn)行過(guò);且只記錄了GET/POST兩種請(qǐng)求方式,其它請(qǐng)求方式以及完整代碼需要參考第二篇文章
到此這篇關(guān)于vue處理get/post的http請(qǐng)求的實(shí)例的文章就介紹到這了,更多相關(guān)vue get/post的http請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue2+electron項(xiàng)目搭建過(guò)程
文章介紹了如何使用Vue CLI 2搭建Vue項(xiàng)目,并添加Electron插件vue-cli-plugin-electron-builder來(lái)創(chuàng)建一個(gè)Vue+Electron項(xiàng)目,文章還提供了啟動(dòng)和打包項(xiàng)目的方法,并提示了下載國(guó)內(nèi)網(wǎng)較慢的文件時(shí)可以先注釋掉代碼進(jìn)行調(diào)試,感興趣的朋友一起看看吧2025-01-01
vue-cli創(chuàng)建vue項(xiàng)目的詳細(xì)步驟記錄
vue.cli是vue中的項(xiàng)目構(gòu)造工具,是一個(gè)npm包,需要安裝node.js和 git才能用,下面這篇文章主要給大家介紹了關(guān)于利用vue-cli創(chuàng)建vue項(xiàng)目的詳細(xì)步驟,需要的朋友可以參考下2022-06-06
VUE v-model表單數(shù)據(jù)雙向綁定完整示例
這篇文章主要介紹了VUE v-model表單數(shù)據(jù)雙向綁定,結(jié)合完整實(shí)例形式分析了vue.js實(shí)現(xiàn)表單數(shù)據(jù)雙向綁定相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
vue同一個(gè)瀏覽器登錄不同賬號(hào)數(shù)據(jù)覆蓋問(wèn)題解決方案
同一個(gè)瀏覽器登錄不同賬號(hào)session一致,這就導(dǎo)致后面登錄的用戶(hù)數(shù)據(jù)會(huì)把前面登錄的用戶(hù)數(shù)據(jù)覆蓋掉,這個(gè)問(wèn)題很常見(jiàn),當(dāng)前我這邊解決的就是同一個(gè)瀏覽器不同窗口只能登錄一個(gè)用戶(hù),對(duì)vue同一個(gè)瀏覽器登錄不同賬號(hào)數(shù)據(jù)覆蓋問(wèn)題解決方法感興趣的朋友一起看看吧2024-01-01
vue+axios?methods方法return取不到值問(wèn)題及解決
這篇文章主要介紹了vue+axios?methods方法return取不到值問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue3通過(guò)hooks方式封裝節(jié)流和防抖的代碼詳解
vue3 中的 hooks 就是函數(shù)的一種寫(xiě)法,就是將文件的一些單獨(dú)功能的js代碼進(jìn)行抽離出來(lái),放到單獨(dú)的js文件中,或者說(shuō)是一些可以復(fù)用的公共方法/功能,本文給大家介紹了Vue3通過(guò)hooks方式封裝節(jié)流和防抖,需要的朋友可以參考下2024-10-10
Vuex中this.$store.commit()和this.$store.dispatch()區(qū)別說(shuō)明
這篇文章主要介紹了Vuex中this.$store.commit()和this.$store.dispatch()區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue常見(jiàn)報(bào)錯(cuò)以及解決方案實(shí)例總結(jié)
最近做了一個(gè)比較老的vue項(xiàng)目,啟動(dòng)居然各種報(bào)錯(cuò),下面這篇文章主要給大家介紹了關(guān)于Vue常見(jiàn)報(bào)錯(cuò)以及解決方案的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
vue3+elementPlus項(xiàng)目支持設(shè)置默認(rèn)附件方式
這篇文章主要介紹了vue3+elementPlus項(xiàng)目支持設(shè)置默認(rèn)附件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03

