axios請求方式和配置項

2021-10-13 07:52:07 字數 2575 閱讀 5874

npm i axios

import axios from "axios"

get請求
axios(config)

axios(url,config)

axios.get(url,config)

post請求
axios(config)

axios(url,config)

axios.post(url,data,config)

put請求
axios(config)

axios(url,config)

axios.put(url,data,config)

patch請求
axios(config)

axios(url,config)

axios.patch(url,data,config)

delete請求
axios(config)

axios(url,config)

axios.delete(url,config)

const body =

await promise.

all(

[多個axios例項函式的執行結果]

)//body 陣列 ; [第乙個axios例項對應的結果,第二個axios例項對應的結果]

axios.

all(

[多個axios例項函式的執行結果]).

then

(axios.

spread

((a,b)

=>

))

var axiosinstance = axios.

create

(config)

//config:公共的config(例項級別)

axiosinstance

(config)

//請求私有的config

axiosinstance

(url,config)

//請求私有的config

axiosinstance.

get(url,config)

//請求私有的config

axiosinstance.

post

(url,data,config)

//請求私有的config

全域性配置 : axios.defaults.baseurl=

""例項配置 :

var axiosins = axios.

create()

私有配置

await

axiosins()

私有配置 > 例項配置 > 全域性配置

,// `status` 來自伺服器響應的 http 狀態碼

status:

200,

// `statustext` 來自伺服器響應的 http 狀態資訊

statustext:

'ok'

,// `headers` 伺服器響應的頭

headers:

,// `config` 是為請求提供的配置資訊

}

例項&函式都可以擁有***

// 新增請求***

axiosinstance.interceptors.request.

use(

function

(config));

// 新增響應***

axiosinstance.interceptors.response.

use(

function

(response)

,function

(error)

);

method:

請求方式

(預設get

)baseurl:

基位址(一定是要抽象成公共配置的)

url :

後台介面的位址

(不要包含基位址)

data :body資料;

是乙個物件

(put patch post請求才能使用)

params :url中的query資料;

是乙個物件

(get

delete put patch post請求才能使用)

headers : 請求頭

timeout: 超時時間

axios常用配置項

1 url 必寫 請求位址 2 method 請求方法,預設是get 3 baseurl 常用 baseurl會新增到url前 url是絕對位址除外 4 transformrequest transformrequest選項允許我們在請求傳送到伺服器之前對請求的資料做出一些改動 該選項只適用於以下請...

axios常規配置項

常規配置項 axios.defaults.baseurl 請求伺服器具體的位址 axios.defaults.withcredentials true 在跨域中允許攜帶憑證 axios.defaults.header content type 請求 當我們通過porps請求向伺服器發請求的時候,能攔...

axios的配置項

最近在學習vue,涉及到axios的ajax操作,記錄一下相關config,方便日後查閱 transformresponse 允許在 then catch之前對響應資料進行更改 transformresponse function data headers 是要傳送的自定義 headers head...