axios學習筆記

2021-10-07 14:54:07 字數 3818 閱讀 9553

二.axios的post方式請求介面

三.axios的併發

四.axios的自定義例項

五.axios***

使用axios前要引入axios檔案

//1

axios.

get(

'url'

)//獲取資料

.then

(res => console.

log(res)

)//顯示資料

.then

(err => promise.

reject

(err)

)//報錯處理

//2axios()

.then

(res => console.

log(res )).

catch

(err => promise.

reject

(err)

)

axios.

get(

'url',}

).then

(res => console.

log(res)).

catch

(err => promise.

reject

(err)

)

axios.

post

('url',)

.then

(res => console.

log(res)).

catch

(err => promise.

reject

(err)

)

const p =

newurlsearchparams()

p.('username',''

)//提交的資料

p.('password',''

) axios.

post

('url'

,p,}).

then

(res => console.

log(res)).

catch

(err => promise.

reject

(err)

)

單個檔案提交
const p =

newformdata()

p.('file'

,e.target.files[0]

)//檔案

axios.

post

('url'

,p,}).

then

(res => console.

log(res)).

catch

(err => promise.

reject

(err)

)

多個檔案提交
const p =

newformdata()

p.('filename',''

)//檔名

p.('file'

,e.target.files[0]

)//檔案

axios.

post

('url'

,p,}).

then

(res => console.

log(res)).

catch

(err => promise.

reject

(err)

)

>

"send"

> 傳送 <

/button>

"sendmore"

> 執行多個併發請求 <

/button>

<

/div>

<

/body>

"../../lib/vue.js"

>

<

/script>

//! 同時發兩個資料請求,如何做?

newvue()

const p2 =

newpromise

((resolve,reject)

=>

,3000)}

) axios.

all(

[p1,p2]).

then

(axios.

spread

(function

(acct, perms)))

;},send()

)const p2 =

newpromise

((resolve,reject)

=>

,3000)}

)

axios.

all(

[p1,p2]).

then

(res => console.

log( res )

)//得到的是陣列}}

})<

/script>

const ins = axios.

create(,

})

.loading-box

<

/style>

<

/head>

="loading-box"

> 載入中 <

/div>

>

"text" v-model=

"username" placeholder=

"請輸入使用者名稱"

>

"text" v-model=

"password" placeholder=

"請輸入密碼"

>

"postform"

> 動態資料請求 - post-表單提交 <

/button>

<

/div>

<

/body>

"">

<

/script>

"../../lib/vue.js"

>

<

/script>

const loadingbox = document.

queryselector

('.loading-box'

) loadingbox.style.lineheight = document.documentelement.clientheight +

'px'

const ins = axios.

create(}

) ins.interceptors.request.

use(

function

(config)

,function

(err)

)// 響應***

ins.interceptors.response.

use(

function

(res)

,function

(err)

)new

vue(

, methods:).

then

(res => console.

log(res)).

catch

(err => promise.

reject

(err))}

,}})

<

/script>

axios學習筆記

預設是get請求,params是為get請求傳參時使用 axios then res axios.all axios.all axios axios then result result 為第乙個請求返回的資料和第二個請求返回資料的陣列集合 axios.all axios axios then ax...

axios學習筆記

它是乙個功能強大的網路請求庫 script src script axios.get 位址?key value key2 values then function response function err axios.get 位址?查詢字串 then function response funct...

axios學習筆記 2

axios.create 請求頭設定 params 請求引數拼接在url上 data 請求引數放在請求體上 axios 在請求或響應被處理前攔截 axios.interceptors.request.use config err 實際開發的例子,加上token let instance axios....