axios使用方法學習筆記

2021-10-07 02:31:41 字數 3603 閱讀 6368

axios官網

axios是由promise封裝的乙個http庫,它的主要特點是

1:從瀏覽器中建立xmlhttprequests

2:支援promise api

3:攔截請求和響應

4:轉換請求資料和響應資料

5:自動轉換json資料

6:客戶端支援防禦xsrf

參考:axios,ajax,jquery ajax,axios和fetch的區別

和ajax的區別是設計模式不同,ajax的設計模式是mvc,而axios的設計模式是mvvm

1: axios預設使用的請求方式是get請求

""

>

<

/script>

axios()

.then

(res=>

)<

/script>

2: 使用get請求方式
不帶引數

""

>

<

/script>

axios()

.then

(res=>).

catch

(err=>

)//catch通常是捕獲客戶端請求格式不正確所導致的錯誤

<

/script>

簡便寫法

axios.

get(

'介面文件位址').

then

(res=>).

catch

(err=>

)

帶引數,使用params傳參

""

>

<

/script>

axios(}

).then

(res=>).

catch

(err=>

)<

/script>

簡便寫法

axios.

get(

'介面文件位址',})

.then

(res=>).

catch

(err=>

)

3:使用post請求
""

>

<

/script>

axios()

.then

(res=>).

catch

(err=>

)<

/script>

也可以

axios.

post

('介面文件位址').

then

(res=>).

catch

(err=>

)

""

>

<

/script>

axios(}

).then

(res=>

)<

/script>

""

>

<

/script>

axios(}

).then

(res=>

)<

/script>

簡便寫法

axios.

post

('介面文件位址',"id=2&name=張三").

then

(res=>).

.catch

(err=>

)

4:併發請求
axios.

all(

[axios.

get(

'介面文件位址'

),axios.

get(

'介面文件位址',})

]).then

(res=>).

catch

(err=>

)

將兩個請求的資料分隔開

axios.

all(

[axios.

get(

'介面文件位址'

),axios.

get(

'介面文件位址',})

]).then

(res=>)}

).catch

(err=>

)

5:全域性配置

//全域性配置

axios.defaults.baseurl=

'介面文件基位址';

axios.defaults.timeout=

5000

;//超出5秒就終止請求

//實際呼叫

axios.

get(

'介面位址').

then

(res=>).

catch

(err=>

)

6:axios例項

let newvar=axios.

create()

;//例項建立

newvar()

.then

(res=>).

catch

(err=>

)<

/script>

axios例項可以同時建立多個

7:axios請求***

axios***分為請求***和響應***,根據js自上而下執行的規則,使用請求***我們可以在發起請求時新增動畫特效,強制登陸等操作

axios.interceptors.request.

use(config=>

,err=>)

axios.

get(

'介面文件位址').

then

(res=>

)<

/script>

axios.interceptors.response.

use(config=>

,err=>)

axios.

get(

'介面文件位址').

then

(res=>

)<

/script>

axios在vue中的封裝
//在檔案request.js中封裝

export

function

request

(config));

return

newvar

(config);}

//呼叫方法

import

from

'request.js的絕對位址'

request()

.then

(res=>

)

參考:axios從入門到入土

php cookie使用方法學習筆記分享

php setcookie 函式向客戶端傳送乙個 http cookie。cookie 是由伺服器傳送到瀏覽器的變數。cookie 通常是伺服器嵌入到使用者計算機中的小文字檔案。每當計算機通過瀏覽器請求乙個頁面,就會傳送這個 cookie。cookie 的名稱指定為相同名稱的變數。例如,如果被傳送的...

excel vba 字典使用方法學習

字典的索引 keys 不能重複,常用字典來去重,字典是乙個二維陣列,其中keys不重複,每個keys對應乙個值 item 值可以重複。vba中常用字典代替vlookup。字典學習 宣告本模組中預設的比較方式,只能是text或者binary 如果不宣告的的話,預設為二進位制比較,但是對vba字典中的比...

linux下vi的使用方法學習

操作內容 1.在 root 目錄下建乙個名為 vi 的目錄。mkdir root vi 2.進入 vi 目錄。cd root vi 3.將檔案 etc inittab 複製到 vi 目錄下。cp etc inittab 4.使用vi開啟 vi 目錄下的inittab。vi inittab 5.設定行...