欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue處理get/post的http請(qǐng)求的實(shí)例

 更新時(shí)間:2022年03月01日 09:17:20   作者:weixin_46353030  
本文主要介紹了vue處理get/post的http請(qǐng)求的實(shí)例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、使用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)求后做一些處理

來自keepfool文章

用法

//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ì)象:

菜鳥教程

參考文章

菜鳥教程

完整代碼示例及其他請(qǐng)求方法的使用

代碼是參考上面兩篇文章寫出來的,沒有實(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)文章

最新評(píng)論