最近在學習vue,涉及到axios的ajax操作,記錄一下相關Config,方便日後查閱 { // `url`是將用於請求的伺服器URL url: '/user', // `method`是發出請求時使用的請求方法 method: 'get', // 預設 // `baseURL`將被添加到`url ...
最近在學習vue,涉及到axios的ajax操作,記錄一下相關Config,方便日後查閱
{
// `url`是將用於請求的伺服器URL
url: '/user',
// `method`是發出請求時使用的請求方法
method: 'get', // 預設
// `baseURL`將被添加到`url`前面,除非`url`是絕對的。
// 可以方便地為 axios 的實例設置`baseURL`,以便將相對 URL 傳遞給該實例的方法。
baseURL: 'https://some-domain.com/api/',
// `transformRequest`允許在請求數據發送到伺服器之前對其進行更改
// 這隻適用於請求方法'PUT','POST'和'PATCH'
// 數組中的最後一個函數必須返回一個字元串,一個 ArrayBuffer或一個 Stream
transformRequest: [function (data) {// 做任何你想要的數據轉換
return data;
}],
// `transformResponse`允許在 then / catch之前對響應數據進行更改
transformResponse: [function (data) {// 做任何你想要的數據轉換
return data;
}],
// `headers`是要發送的自定義 headers
headers: {'X-Requested-With': 'XMLHttpRequest'},
// `params`是要與請求一起發送的URL參數
// 必須是純對象或URLSearchParams對象
params: {
ID: 12345
},
// `paramsSerializer`是一個可選的函數,負責序列化`params`
// (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
paramsSerializer: function(params) {
return Qs.stringify(params, {arrayFormat: 'brackets'})
},
// `data`是要作為請求主體發送的數據
// 僅適用於請求方法“PUT”,“POST”和“PATCH”
// 當沒有設置`transformRequest`時,必須是以下類型之一:
// - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
// - Browser only: FormData, File, Blob
// - Node only: Stream
data: {
firstName: 'Fred'
},
// timeout指定請求超時之前的毫秒數。
// 如果請求的時間超過timeout,請求將被中止。
timeout: 1000,
// withCredentials指示是否跨站點訪問控制請求
// should be made using credentials
withCredentials: false, // default
// `adapter'允許自定義處理請求,這使得測試更容易。
// 返回一個promise並提供一個有效的響應(參見[response docs](#response-api))
adapter: function (config) {
/* ... */
},
// `auth'表示應該使用 HTTP 基本認證,並提供憑據。
// 這將設置一個`Authorization'頭,覆蓋任何現有的`Authorization'自定義頭,使用`headers`設置。
auth: {
username: 'janedoe',
password: 's00pers3cret'
},
// “responseType”表示伺服器將響應的數據類型
// 包括 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
responseType: 'json', // 預設
//`xsrfCookieName`是要用作 xsrf 令牌的值的cookie的名稱
xsrfCookieName: 'XSRF-TOKEN', // 預設
// `xsrfHeaderName`是攜帶xsrf令牌值的http頭的名稱
xsrfHeaderName: 'X-XSRF-TOKEN', // 預設
// `onUploadProgress`允許處理上傳的進度事件
onUploadProgress: function (progressEvent) {// 使用本地 progress 事件做任何你想要做的
},
// `onDownloadProgress`允許處理下載的進度事件
onDownloadProgress: function (progressEvent) {// 使用本地 progress 事件做任何你想要做的
},
// `maxContentLength`定義允許的http響應內容的最大大小
maxContentLength: 2000,
// `validateStatus`定義是否解析或拒絕給定的promise
// HTTP響應狀態碼。如果`validateStatus`返回`true`(或被設置為`null` promise將被解析;否則,promise將被拒絕。
validateStatus: function (status) {
return status >= 200 && status < 300; // 預設
},
// `maxRedirects`定義在node.js中要遵循的重定向的最大數量。
// 如果設置為0,則不會遵循重定向。
maxRedirects: 5, // 預設
// `httpAgent`和`httpsAgent`用於定義在node.js中分別執行http和https請求時使用的自定義代理。
// 允許配置類似`keepAlive`的選項,
// 預設情況下不啟用。
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
// 'proxy'定義代理伺服器的主機名和埠
// `auth`表示HTTP Basic auth應該用於連接到代理,並提供credentials。
// 這將設置一個`Proxy-Authorization` header,覆蓋任何使用`headers`設置的現有的`Proxy-Authorization` 自定義 headers。
proxy: {
host: '127.0.0.1',
port: 9000,
auth: : {
username: 'mikeymike',
password: 'rapunz3l'
}
},
// “cancelToken”指定可用於取消請求的取消令牌
// (see Cancellation section below for details)
cancelToken: new CancelToken(function (cancel) {
})
}