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來(lái)實(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 ,從這方面來(lái)講會(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 來(lái)傳遞參數(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使用iframe嵌入ureport2設(shè)計(jì)器,解決預(yù)覽時(shí)NullPointerException異常問題
這篇文章主要介紹了vue3使用iframe嵌入ureport2設(shè)計(jì)器,解決預(yù)覽時(shí)NullPointerException異常問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue.js動(dòng)態(tài)修改background-image問題
這篇文章主要介紹了vue.js動(dòng)態(tài)修改background-image問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue+element樹組件 實(shí)現(xiàn)樹懶加載的過程詳解
這篇文章主要介紹了vue+element樹組件 實(shí)現(xiàn)樹懶加載的過程,本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10vue從倉(cāng)庫(kù)state中取不到數(shù)據(jù)的問題
這篇文章主要介紹了vue從倉(cāng)庫(kù)state中取不到數(shù)據(jù)的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04Vue 路由返回恢復(fù)頁(yè)面狀態(tài)的操作方法
在使用 Vue 開發(fā)前端的時(shí)候遇到一個(gè)場(chǎng)景在首頁(yè)進(jìn)行一些數(shù)據(jù)搜索,點(diǎn)擊搜索結(jié)果進(jìn)入詳情頁(yè)面,瀏覽詳情頁(yè)后返回主頁(yè),所以需要在返回后恢復(fù)跳轉(zhuǎn)前的頁(yè)面參數(shù)狀態(tài),今天通過本文給大家分享Vue 路由頁(yè)面狀態(tài)返回的操作方法,一起看看吧2021-07-07在Vue中實(shí)現(xiàn)隨hash改變響應(yīng)菜單高亮
這篇文章主要介紹了在Vue中實(shí)現(xiàn)隨hash改變響應(yīng)菜單高亮的方法,文中還通過實(shí)例代碼給大家介紹了vue關(guān)于點(diǎn)擊菜單高亮與組件切換的相關(guān)知識(shí),需要的朋友可以參考下2020-03-03