Vue Axios基本用法

2021-08-31 09:37:33 字數 4185 閱讀 5749

axios是乙個基於promise的http庫,可以用在瀏覽器和node.js中.

通過npm 使用axios

npm install axios
也可以在專案中使用cdn引入axios

get請求

// 不指定post和get方式,會預設為get方式

axios

('/home');

// 指定id的get請求

axios.

get(

'/home?id=001').

then

(function

(res)).

catch

(function

(error));

// 還可以用下面的形式

axios.

get(

'/home',}

).then

(function

(res)).

catch

(function

(error)

);

post請求
axios.

post

('/home',)

.then

(function

(res)).

catch

(function

(error)

)

執行多個併發請求

執行多個併發請求時需要用到 axios.all 和 axios.spread

function

getuseraccount()

function

getuserpermissions()

axios.

all(

[getuseraccount()

,getuserpermissions()

]).then

(axios.

spread

(function

(acct, perms)))

;

自定義配置

axios.create可以建立自定義的請求例項,可以在自定義的例項中重寫各個配置項,以滿足自己的需求.

以下是自定義配置選項:

],

// `transformresponse` 在傳遞給 then/catch 前,允許修改響應資料

transformresponse:

[function

(data)],

// `headers` 是即將被傳送的自定義請求頭

headers:

,// `params` 是即將與請求一起傳送的 url 引數

// 必須是乙個無格式物件(plain object)或 urlsearchparams 物件

params:

,// `paramsserializer` 是乙個負責 `params` 序列化的函式

// (e.g.

paramsserializer:

function

(params))}

,// `data` 是作為請求主體被傳送的資料

// 只適用於這些請求方法 'put', 'post', 和 'patch'

// 在沒有設定 `transformrequest` 時,必須是以下型別之一:

// - string, plain object, arraybuffer, arraybufferview, urlsearchparams

// - 瀏覽器專屬:formdata, file, blob

// - node 專屬: stream

data:

,// `timeout` 指定請求超時的毫秒數(0 表示無超時時間)

// 如果請求話費了超過 `timeout` 的時間,請求將被中斷

timeout:

1000

,// `withcredentials` 表示跨域請求時是否需要使用憑證

withcredentials:

false

,// 預設的

// `adapter` 允許自定義處理請求,以使測試更輕鬆

// 返回乙個 promise 並應用乙個有效的響應 (查閱 [response docs](#response-api)).

adapter:

function

(config)

,// `auth` 表示應該使用 http 基礎驗證,並提供憑據

// 這將設定乙個 `authorization` 頭,覆寫掉現有的任意使用 `headers` 設定的自定義 `authorization`頭

auth:

,// `responsetype` 表示伺服器響應的資料型別,可以是 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'

responsetype:

'json'

,// 預設的

// `xsrfcookiename` 是用作 xsrf token 的值的cookie的名稱

xsrfcookiename:

'xsrf-token'

,// default

// `xsrfheadername` 是承載 xsrf token 的值的 http 頭的名稱

xsrfheadername:

'x-xsrf-token'

,// 預設的

// `onuploadprogress` 允許為上傳處理進度事件

onuploadprogress:

function

(progressevent)

, ondownloadprogress:

function

(progressevent)

,// `maxcontentlength` 定義允許的響應內容的最大尺寸

maxcontentlength:

2000

,// `validatestatus` 定義對於給定的http 響應狀態碼是 resolve 或 reject promise 。如果 `validatestatus` 返回 `true` (或者設定為 `null` 或 `undefined`),promise 將被 resolve; 否則,promise 將被 rejecte

validatestatus:

function

(status)

,// `maxredirects` 定義在 node.js 中 follow 的最大重定向數目

// 如果設定為0,將不會 follow 任何重定向

maxredirects:5,

// 預設的

// `httpagent` 和 `httpsagent` 分別在 node.js 中用於定義在執行 http 和 https 時使用的自定義**。允許像這樣配置選項:

// `keepalive` 預設沒有啟用

,// 'proxy' 定義**伺服器的主機名稱和埠

// `auth` 表示 http 基礎驗證應當用於連線**,並提供憑據

// 這將會設定乙個 `proxy-authorization` 頭,覆寫掉已有的通過使用 `header` 設定的自定義 `proxy-authorization` 頭。

proxy:},

// `canceltoken` 指定用於取消請求的 cancel token

canceltoken:

newcanceltoken

(function

(cancel)

)}

***
// 新增請求***

axios.interceptors.request.

use(

function

(config)

,function

(error));

// 新增響應***

axios.interceptors.response.

use(

function

(response)

,function

(error)

);

python基本用法 Python基本用法總結

python 的設計具有很強的可讀性,易於使用。這裡對python基本用法做乙個總結介紹。一 變數型別 1 變數賦值 python中的變數賦值不需要型別宣告,每個變數在使用前都必須賦值,變數賦值之後才會被建立。使用等號進行變數的賦值。2 多個變數賦值 為多個變數賦值時或進行列表內資料的交換時,可以使...

vue axios 請求封裝

import axios from axios import router from router index import from element ui import from utils loading import from utils auth import qs from qs 引入qs...

封裝vue axios請求

util.js import axios from axios import qs from qs import from vant h5用的這個 const apiurl xx 訪問介面位址 設定超時時間 請求頭等 axios.defaults.timeout 30000 axios.defaul...