這些是創(chuàng)建請(qǐng)求時(shí)可以用的配置選項(xiàng)。只有 url
是必需的,如果沒(méi)有指定 method
,請(qǐng)求將默認(rèn)使用 get
方法。
{
// url 是用于請(qǐng)求的服務(wù)器 URL
url: '/user'
// method 是創(chuàng)建請(qǐng)求時(shí)使用的方法
method: 'get', // default
// baseURL 將自動(dòng)加在 url 前面,除非 url 是一個(gè)絕對(duì) URL
// 它可以通過(guò)設(shè)置一個(gè) baseURL 便于為 axios 實(shí)例的方法傳遞相對(duì) URL
baseURL: 'https://some-domain.com/api/',
// transformRequest 允許在向服務(wù)器發(fā)送前,修改請(qǐng)求數(shù)據(jù)
// 只能用在 PUT、POST 和 PATCH 這幾個(gè)請(qǐng)求方法
// 后面數(shù)組中的函數(shù)必須返回一個(gè)字符串,或 ArrayBuffer,或 Stream
transformRequest: [function (data, headers) {
// 對(duì) data 進(jìn)行任意轉(zhuǎn)換處理
return data;
}],
// transformResponse 在傳遞給 then/catch 前,允許修改響應(yīng)數(shù)據(jù)
transformResponse: [function (data) {
// 對(duì) data 進(jìn)行任意轉(zhuǎn)換處理
return data;
}],
// headers 是即將被發(fā)送的自定義請(qǐng)求頭
headers: {'X-Requested-With': 'XMLHttpRequest'},
// params 是即將與請(qǐng)求一起發(fā)送的 URL 參數(shù)
// 必須是一個(gè)無(wú)格式對(duì)象 (plain object) 或 URLSearchParams 對(duì)象
params: {
ID: 12345
},
// paramsSerializer 是一個(gè)負(fù)責(zé) params 序列化的函數(shù)
// (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
paramsSerializer: function (params) {
return QS.stringify(params, {arrayFormat: 'brackets'})
},
// data 是作為請(qǐng)求主體被發(fā)送的數(shù)據(jù)
// 只適用于這些請(qǐng)求方法 PUT、POST 和 PATCH
// 在沒(méi)有設(shè)置 transformRequest 時(shí),必須是以下類(lèi)型之一:
// - string,plain object,ArrayBuffer,ArrayBufferView,URLSearchParams
// - 瀏覽器專(zhuān)屬:FormData,F(xiàn)ile,Blob
// - Node專(zhuān)屬:Stream
data: {
firstName: 'Fred'
},
// timeout 指定請(qǐng)求超時(shí)的毫秒數(shù)(0 表示無(wú)超時(shí)時(shí)間)
// 如果請(qǐng)求花費(fèi)了超過(guò) timeout 的時(shí)間,請(qǐng)求將被中斷
timeout: 1000,
// withCredentials 表示跨域請(qǐng)求時(shí)是否需要使用憑證
withCredentials: false, // default
// adapter 允許自定義處理請(qǐng)求,以使測(cè)試更輕松
// 返回一個(gè) promise 并應(yīng)用一個(gè)有效的響應(yīng)(查閱 [response docs](#response-api))
adapter: function (config) {
/* ... */
},
// auth 表示應(yīng)該使用 HTTP 基礎(chǔ)驗(yàn)證,并提供憑據(jù)
// 這將設(shè)置一個(gè) Authorization 頭,覆寫(xiě)掉現(xiàn)有的任意使用 headers 設(shè)置的自定義 Authorization 頭
auth: {
username: 'janedoe',
password: 's00pers3cret'
},
// responseType 表示服務(wù)器響應(yīng)的數(shù)據(jù)類(lèi)型,可以是 arraybuffer、blob、document、json、text、stream
responseType: 'json', // default
// responseEncoding 表示對(duì)響應(yīng)的編碼
// Note:對(duì)于 responseType 為 stream 或 客戶(hù)端請(qǐng)求會(huì)忽略
responseEncoding: 'utf-8',
// xsrfCookieName 是用作 xsrf token 值的 cookie 名稱(chēng)
xsrfCookieName: 'XSRF-TOKEN', // default
// xsrfHeaderName 是 xsrf token 值的 http 頭名稱(chēng)
xsrfHeaderName: 'X-XSRF-TOKEN', // default
// onUploadProgress 允許為上傳處理進(jìn)度事件
onUploadProgress: function (progressEvent) {
// ... ...
},
// onDownloadProgress 允許為下載處理進(jìn)度事件
onDownloadProgress: function (progressEvent) {
// ... ...
},
// maxContentLength 定義允許的響應(yīng)內(nèi)容的最大尺寸
maxContentLength: 2000,
// validateStatus 定義對(duì)于給定的 HTTP 響應(yīng)狀態(tài)碼是 resolve 或 reject promise。
// 如果 validateStatus 返回 true (或者設(shè)置為 null 或 undefined),promise 將被 resolve,否則 promise 將被 reject
validateStatus: function (status) {
return status >= 200 && status < 300; // default
},
// maxRedirects 定義在 node.js 中 follow 的最大重定向數(shù)目
// 如果設(shè)置為 0,將不會(huì) follow 任何重定向
maxRedirects: 5,
// socketPath 用于在 node.js 中定義 UNIX Socket
// e.g. '/var/run/docker.sock' to send requests to the docker daemon.
// 只能指定 socketPath 或 proxy,如果兩者同時(shí)指定,將使用 socketPath
socketPath: null,
// httpAgent 和 httpsAgent 分別在 node.js 中用于定義在執(zhí)行 http 和 https 時(shí)使用的自定義代理。
// 允許像這樣配置選項(xiàng)。keepAlive 默認(rèn)沒(méi)有啟用
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
// proxy 定義代理服務(wù)器的主體名稱(chēng)和端口
// auth 表示 HTTP 基礎(chǔ)驗(yàn)證應(yīng)當(dāng)用于連接代理,并提供憑據(jù)
// 這將會(huì)設(shè)置一個(gè) Proxy-Authorization 頭,覆寫(xiě)掉已有的通過(guò)使用 header 設(shè)置的自定義 Proxy-Authorization 頭
proxy: {
host: '127.0.0.1',
port: 9000,
auth: {
username: 'mikeymike',
password: 'rapunz31'
}
},
// cancelToken 指定用于取消請(qǐng)求的 cancel token
cancelToken: new CancelToken(function (cancel) {
// ... ...
})
}
更多建議: