vue處理get/post的http請求的實例
一、使用Vue.http/this.$http
在發(fā)起請求的時候,為了減少作用域鏈的搜索,建議使用一個局部變量來接受this
1. GET請求
// 基于全局Vue對象使用http Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback); // 在一個Vue實例內使用$http this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);
示例
//不帶參數(shù)的get請求 this.$http.get('/someUrl').then(function(res){ console.log('請求成功處理'); },function(res){ console.log('請求失敗處理'); }); //需要傳遞數(shù)據(jù)的get請求 this.$http.get('/someUrl',{param:jsonData}).then(function(res){ console.log('請求成功處理'); },function(res){ console.log('請求失敗處理'); }); //ES6的Lambda寫法 this.$http.get('/someUrl', [options]).then((response) => { console.log('請求成功處理'); }, (response) => { console.log('請求失敗處理'); });
2.POST請求
post 發(fā)送數(shù)據(jù)到后端,需要第三個參數(shù) {emulateJSON:true}
。
emulateJSON 的作用: 如果Web服務器無法處理編碼為 application/json 的請求,你可以啟用 emulateJSON 選項。啟用該選項后,請求會以application/x-www-form-urlencoded
作為MIME type,就像普通的HTML表單一樣。
// 基于全局Vue對象使用http Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback); // 在一個Vue實例內使用$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服務,resource服務包含以下幾種默認的action:
get: {method: 'GET'}, save: {method: 'POST'}, query: {method: 'GET'}, update: {method: 'PUT'}, remove: {method: 'DELETE'}, delete: {method: 'DELETE'}
訪問resource對象的兩種方式
:
//全局訪問 Vue.resource //實例訪問 this.$resource
GET請求
//使用一個局部變量來接受this var vm = this; this.$resource('apiUrl').get().then((response) => { console.log("調用成功"); }) .catch(function(response) { console.log("調用失敗"); }) }
POST請求
使用save
方法發(fā)送POST請求
//使用一個局部變量來接受this var vm = this; this.$resource('apiUrl').save('apiUrl',Target).then((response) => { console.log("調用成功"); }) .catch(function(response) { console.log("調用失敗"); }) }
inteceptor – 在請求前和請求后附加行為
攔截器可以在請求發(fā)送前和發(fā)送請求后做一些處理
用法
//Lambda函數(shù)寫法 Vue.http.interceptors.push((request, next) => { // ... // 請求發(fā)送前的處理邏輯 // ... next((response) => { // ... // 請求發(fā)送后的處理邏輯 // ... // 根據(jù)請求的狀態(tài),response參數(shù)會返回給successCallback或errorCallback return response }) }) //普通寫法 Vue.http.interceptors.push(function(request, next) { // ... // 請求發(fā)送前的處理邏輯 // ... next(function(response) { // ... // 請求發(fā)送后的處理邏輯 // ... // 根據(jù)請求的狀態(tài),response參數(shù)會返回給successCallback或errorCallback return response }) })
實例 – 為所有的請求處理加一個loading
請求發(fā)送前顯示loading,接收響應后隱藏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ā)起請求時顯示 <loading v-show="showLoading"</loading> //modal-dialog 請求失敗時顯示 <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 種請求 API(REST 風格):
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 名稱是標準的 HTTP 方法。其中,options參數(shù)說明如下:
可以通過如下屬性和方法處理一個請求獲取到的響應對象:
參考文章
代碼是參考上面兩篇文章寫出來的,沒有實際運行過;且只記錄了GET/POST兩種請求方式,其它請求方式以及完整代碼需要參考第二篇文章
到此這篇關于vue處理get/post的http請求的實例的文章就介紹到這了,更多相關vue get/post的http請求內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue.js實現(xiàn)在下拉列表區(qū)域外點擊即可關閉下拉列表的功能(自定義下拉列表)
這篇文章主要介紹了Vue.js實現(xiàn)在下拉列表區(qū)域外點擊即可關閉下拉列表的功能(自定義下拉列表) ,需要的朋友可以參考下2017-05-05vue3-vue-router創(chuàng)建靜態(tài)路由和動態(tài)路由方式
這篇文章主要介紹了vue3-vue-router創(chuàng)建靜態(tài)路由和動態(tài)路由方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10Vue3結合TypeScript項目開發(fā)實戰(zhàn)記錄
聽說有的公司已經(jīng)開始用vue3了 趕緊打開B站學一下,下面這篇文章主要給大家介紹了關于Vue3結合TypeScript項目開發(fā)實戰(zhàn)的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2021-09-09