vue中axios處理http發(fā)送請(qǐng)求的示例(Post和get)
本文介紹了vue中axios處理http發(fā)送請(qǐng)求的示例(Post和get),分享給大家,具體如下:
https://github.com/mzabriskie/axios#using-applicationx-www-form-urlencoded-format axios文檔
在處理http請(qǐng)求方面,已經(jīng)不推薦使用vue-resource了,而是使用最新的axios,下面做一個(gè)簡(jiǎn)單的介紹。
安裝
使用node
npm install axios
使用cdn
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
基本使用方法
get請(qǐng)求
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
// Optionally the request above could also be done as
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
Post請(qǐng)求
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
同時(shí)執(zhí)行多個(gè)請(qǐng)求
function getUserAccount() {
return axios.get('/user/12345');
}
function getUserPermissions() {
return axios.get('/user/12345/permissions');
}
axios.all([getUserAccount(), getUserPermissions()])
.then(axios.spread(function (acct, perms) {
// Both requests are now complete
}));
這個(gè)的使用方法其實(shí)和原生的ajax是一樣的,一看就懂。
使用 application/x-www-urlencoded 形式的post請(qǐng)求:
var qs = require('qs');
axios.post('/bbg/goods/get_goods_list_wechat', qs.stringify({"data": JSON.stringify({
"isSingle": 1,
"sbid": 13729792,
"catalog3": 45908012,
"offset": 0,
"pageSize": 25
})}), {
headers: {
"BBG-Key": "ab9ef204-3253-49d4-b229-3cc2383480a6",
}
})
.then(function (response) {
// if (response.data.code == 626) {
console.log(response);
// }
}).catch(function (error) {
console.log(error);
});
具體使用參考文檔: https://github.com/mzabriskie/axios#using-applicationx-www-form-urlencoded-format
注意: 對(duì)于post請(qǐng)求,一般情況下,第一個(gè)參數(shù)是url,第二個(gè)參數(shù)是要發(fā)送的請(qǐng)求體的數(shù)據(jù),第三個(gè)參數(shù)是對(duì)請(qǐng)求的配置。
另外:axios默認(rèn)是application/json格式的,如果不適用 qs.stringify 這種形式, 即使添加了請(qǐng)求頭 最后的content-type的形式還是 json 的。
對(duì)于post請(qǐng)求,我們也可以使用下面的jquery的ajax來實(shí)現(xiàn):
$.ajax({
url:'api/bbg/goods/get_goods_list_wechat',
data:{
'data': JSON.stringify({
"isSingle": 1,
"sbid": 13729792,
"catalog3": 45908012,
"offset": 0,
"pageSize": 25
})
},
beforeSend: function(request) {
request.setRequestHeader("BBG-Key", "ab9ef204-3253-49d4-b229-3cc2383480a6");
},
type:'post',
dataType:'json',
success:function(data){
console.log(data);
},
error: function (error) {
console.log(err);
},
complete: function () {
}
});
顯然,通過比較,可以發(fā)現(xiàn),jquery的請(qǐng)求形式更簡(jiǎn)單一些,且jqury默認(rèn)的數(shù)據(jù)格式就是 application/x-www-urlencoded ,從這方面來講會(huì)更加方便一些。
另外,對(duì)于兩個(gè)同樣的請(qǐng)求,即使都請(qǐng)求成功了,但是兩者請(qǐng)求得到的結(jié)果也是不一樣的,如下:

不難看到: 使用axios返回的結(jié)果會(huì)比jquery的ajax返回的結(jié)構(gòu)(實(shí)際的結(jié)果)多包裝了一層,包括相關(guān)的config、 headers、request等。
對(duì)于get請(qǐng)求, 我個(gè)人還是推薦使用axios.get()的形式,如下所示:
axios.get('/bbg/shop/get_classify', {
params: {
sid: 13729792
},
headers: {
"BBG-Key": "ab9ef204-3253-49d4-b229-3cc2383480a6"
}
})
.then(function (response) {
if (response.data.code == 130) {
items = response.data.data;
store.commit('update', items);
console.log(items);
}
console.log(response.data.code);
}).catch(function (error) {
console.log(error);
console.log(this);
});
即第一個(gè)參數(shù)是:url, 第二個(gè)參數(shù)就是一個(gè)配置對(duì)象,我們可以在配置對(duì)象中設(shè)置 params 來傳遞參數(shù)。
個(gè)人理解為什么get沒有第二個(gè)參數(shù)作為傳遞的查詢字符串,而post有第二個(gè)參數(shù)作為post的數(shù)據(jù)。
因?yàn)間et可以沒有查詢字符串,也可以get請(qǐng)求,但是post必須要有post的數(shù)據(jù),要不然就沒有使用post的必要了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue使用axios發(fā)送請(qǐng)求簡(jiǎn)單實(shí)現(xiàn)代碼
- Vue axios庫(kù)避免重復(fù)發(fā)送請(qǐng)求的示例介紹
- Vue?axios庫(kù)發(fā)送請(qǐng)求的示例介紹
- Vue使用axios發(fā)送請(qǐng)求并實(shí)現(xiàn)簡(jiǎn)單封裝的示例詳解
- vue項(xiàng)目使用axios發(fā)送請(qǐng)求讓ajax請(qǐng)求頭部攜帶cookie的方法
- Vue.js實(shí)戰(zhàn)之使用Vuex + axios發(fā)送請(qǐng)求詳解
- vue-cli3.0 axios跨域請(qǐng)求代理配置方式及端口修改
- Vue3.0?axios跨域請(qǐng)求代理服務(wù)器配置方式
- Vue利用axios發(fā)送請(qǐng)求并代理請(qǐng)求的實(shí)現(xiàn)代碼
相關(guān)文章
Vue3使用Univer Docs創(chuàng)建在線編輯Excel的示例代碼
本文介紹了如何在Vue3項(xiàng)目中集成UniverDocs,一個(gè)基于Luckysheet的企業(yè)文檔與數(shù)據(jù)協(xié)同解決方案,指導(dǎo)了從安裝到在頁面中使用的步驟,以及注意事項(xiàng),如數(shù)據(jù)格式轉(zhuǎn)換和二次開發(fā)的靈活性,需要的朋友可以參考下2025-04-04
Vue3實(shí)現(xiàn)一個(gè)可左右滑動(dòng)操作組件的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Vue3實(shí)現(xiàn)一個(gè)可左右滑動(dòng)操作組件,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定幫助,感興趣的可以學(xué)一下2022-11-11
Vue雙向數(shù)據(jù)綁定與響應(yīng)式原理深入探究
本節(jié)介紹雙向數(shù)據(jù)綁定以及響應(yīng)式的原理,回答了雙向數(shù)據(jù)綁定和數(shù)據(jù)響應(yīng)式是否相同,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-08-08
vue el-table實(shí)現(xiàn)遞歸嵌套的示例代碼
本文主要介紹了vue el-table實(shí)現(xiàn)遞歸嵌套的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
vuecli3.0腳手架搭建及不同的打包環(huán)境配置vue.config.js的詳細(xì)過程
這篇文章主要介紹了vuecli3.0腳手架搭建及不同的打包環(huán)境配置vue.config.js的詳細(xì)過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01

