vue處理get/post的http請(qǐng)求的實(shí)例
一、使用Vue.http/this.$http
在發(fā)起請(qǐng)求的時(shí)候,為了減少作用域鏈的搜索,建議使用一個(gè)局部變量來接受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寫法 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ù)器無法處理編碼為 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:"菜鳥教程",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提供了另外一種方式訪問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'}
訪問resource對(duì)象的兩種方式
:
//全局訪問 Vue.resource //實(shí)例訪問 this.$resource
GET請(qǐng)求
//使用一個(gè)局部變量來接受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è)局部變量來接受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ù)寫法 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 }) }) //普通寫法 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 名稱是標(biāo)準(zhǔn)的 HTTP 方法。其中,options參數(shù)說明如下:
可以通過如下屬性和方法處理一個(gè)請(qǐng)求獲取到的響應(yīng)對(duì)象:
參考文章
代碼是參考上面兩篇文章寫出來的,沒有實(shí)際運(yùn)行過;且只記錄了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)文章
Vue.js實(shí)現(xiàn)在下拉列表區(qū)域外點(diǎn)擊即可關(guān)閉下拉列表的功能(自定義下拉列表)
這篇文章主要介紹了Vue.js實(shí)現(xiàn)在下拉列表區(qū)域外點(diǎn)擊即可關(guān)閉下拉列表的功能(自定義下拉列表) ,需要的朋友可以參考下2017-05-05vue3-vue-router創(chuàng)建靜態(tài)路由和動(dòng)態(tài)路由方式
這篇文章主要介紹了vue3-vue-router創(chuàng)建靜態(tài)路由和動(dòng)態(tài)路由方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue基礎(chǔ)ESLint?Prettier配置教程詳解
這篇文章主要介紹了vue基礎(chǔ)ESLint?Prettier配置教程詳解,本文使用VsCode?+?Vue?+?ESLint?+?Prettier?實(shí)現(xiàn)代碼格式規(guī)范?+?保存自動(dòng)修復(fù)代碼js+vue2022-07-07Vue3結(jié)合TypeScript項(xiàng)目開發(fā)實(shí)戰(zhàn)記錄
聽說有的公司已經(jīng)開始用vue3了 趕緊打開B站學(xué)一下,下面這篇文章主要給大家介紹了關(guān)于Vue3結(jié)合TypeScript項(xiàng)目開發(fā)實(shí)戰(zhàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09Vue 實(shí)現(xiàn)html中根據(jù)類型顯示內(nèi)容
今天小編大家分享一篇Vue 實(shí)現(xiàn)html中根據(jù)類型顯示內(nèi)容,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10