用於瀏覽器和 Node.js 的 Http 用戶端。
Example - Post
var form_data = new FormData();
form_data.append("Key", 'Value');
const axios = require('axios');
axios({
url: 'url_link',
method: 'post',
data: form_data,
}).then(function (response) {
console.log(response);
var data = response.data;
console.log(data);
}).catch(function (error) {
console.log(error);
}).finally(function () {
console.log('finally')
})
Example - Get
const axios = require('axios');
axios({
url: 'url_link',
method: 'get',
params: {
append: 'test',
}
}).then(function (response) {
console.log(response)
var data = response.data;
console.log(data)
}).catch(function (error) {
console.log(error)
}).finally(function () {
console.log('finally')
})
Example - ProgressBar
const axios = require('axios');
var formData= new FormData(document.querySelector('#upload'))
axios({
url: 'url_link',
method: 'post',
data: formData,
responseType: 'arraybuffer',
onUploadProgress(progressEvt) {
var percentCompleted = Math.round((progressEvt.loaded * 100) / progressEvt.total)
$('#upload-bar').css({'width': percentCompleted + '%'})
$('#upload-bar').attr('aria-valuenow', "" + percentCompleted)
$('#upload-bar').text(percentCompleted + '%')
},
onDownloadProgress(progressEvt) {
var percentCompleted = Math.round((progressEvt.loaded * 100) / progressEvt.total)
$('#download-bar').css({'width': percentCompleted + '%'})
$('#download-bar').attr('aria-valuenow', "" + percentCompleted)
$('#download-bar').text(percentCompleted + '%')
},
}).then(function (res) {
var FileSaver = require('file-saver');
var blob = new Blob([res.data], {type: "octet/stream"});
var disposition = res.headers['content-disposition']
var keyword = 'filename='
var start = disposition.indexOf(keyword) + keyword.length
var filename = disposition.substring(start)
FileSaver.saveAs(blob, filename);
}).catch(function (err) {
console.log(err)
}).finally(function () {
console.log('finally')
})