axios中get請求與post請求的簡單函式封裝

2021-10-09 08:58:05 字數 1119 閱讀 5626

首先講講axios的get請求。

axios.get('/user')

.then( (response) => )

.catch( (error) => );

get請求如何傳參想必大家應該都知道的

一是可以拼接在url上

axios.get('/user?id=12345')

.then( (response) => )

.catch( (error) => );

二是axios可以以鍵值對的形式放入params物件中

axios.get('/user', 

}).then( (response) => )

.catch( (error) => );

axios的post請求

axios的post請求引數可以直接在請求位址後加在乙個物件中

axios.post('/user', )

.then( (response) => )

.catch( (error) => );

今天講的如何將axios的post請求與get請求封裝在一起利用的是axios(config)

1、get請求

axios(

}).then( (response) => )

.catch( (error) => );

2、post請求

axios(

}).then( (response) => )

.catch( (error) => );

根據上面的對比大家應該可以清楚看出get請求與post請求的差異在哪了,那麼我們只要將差異點摘出來就ok了

import axios from 'axios';

export default function(url, method, params = {}) ).then((res) => ).catch((err) => )

}

利用上面的函式我們就可以輕鬆利用這個函式進行axios請求了,今天就講這麼多,希望對大家有所幫助。

axios中get請求與post請求的簡單函式封裝

首先講講axios的get請求。axios.get user then response catch error get請求如何傳參想必大家應該都知道的 一是可以拼接在url上 axios.get user?id 12345 then response catch error 二是axios可以以鍵...

Vue resource中的GET與POST請求

在 vue 2 版本,官方推薦使用 axios 來進行開發,可以閱讀這篇文章來學習 axios 全攻略之基本介紹與使用 get 與 post 前言 vue resource 使用比 jquery 更加簡潔的方式實現了非同步請求功能,還提供了比如 interceptors 這樣處理請求過程中行為的功能...

iOS中網路請求的使用 GET請求與POST請求

get post get和post是兩種最常用的與伺服器進行互動的http方法 get語意是獲取指定url的資源。將資料按照variable value的形式,新增到action所指向的url後面,並且兩者只用 連線,各變數之間使用 連線。貌似不安全,因為在傳輸過程中,資料被放在請求的url中。傳輸...