網(wǎng)站背景如何做網(wǎng)站注冊(cè)
axios請(qǐng)求類型是文件流,但是報(bào)錯(cuò)信息的話沒法顯示,在request.js文件中更改一下request攔截器代碼:
service.interceptors.request.use(config => {
? ? ? ? ......
, error => {
? console.log(error, '報(bào)錯(cuò)報(bào)錯(cuò)')
? // 處理請(qǐng)求錯(cuò)誤
? if (error.response && error.response.data instanceof Blob && error.response.data.type === 'application/json') {
? ? // 如果錯(cuò)誤信息是一個(gè) JSON 格式的 Blob,那么讀取 Blob 的內(nèi)容并解析為 JSON 對(duì)象
? ? return error.response.data.text().then(text => {
? ? ? const json = JSON.parse(text);
? ? ? console.log(json);
? ? ? // 返回一個(gè)包含錯(cuò)誤信息的 Promise 對(duì)象
? ? ? return Promise.reject(json);
? ? });
? }
? // 如果錯(cuò)誤信息不是一個(gè) JSON 格式的 Blob,那么返回原始的錯(cuò)誤對(duì)象
? return Promise.reject(error);
? // Promise.reject(error)
})
?更改響應(yīng)攔截器:
service.interceptors.response.use(res => {
if (res.data) {
? ? ? // 判斷Bolb類型是否有錯(cuò)誤信息 ?錯(cuò)誤信息的話直接彈窗提示
? ? ? if (
? ? ? ? res.data instanceof Blob &&
? ? ? ? res.data.type === 'application/json'
? ? ? ) {
? ? ? ? let reader = new FileReader()
? ? ? ? reader.readAsText(res.data, 'utf-8')
? ? ? ? reader.onload = function (e) {
? ? ? ? ? let data = JSON.parse(e.target.result)
? ? ? ? ? if (data.code !== 200) {
? ? ? ? ? ? MessageBox.confirm(data.data, '文件生成失敗', {
? ? ? ? ? ? ? confirmButtonText: '確定',
? ? ? ? ? ? ? cancelButtonText: '取消',
? ? ? ? ? ? ? type: 'error'
? ? ? ? ? ? }).then(() => {
? ? ? ? ? ? })
? ? ? ? ? }
? ? ? ? }
? ? ? ? return Promise.resolve(res)
? ? ? }
? ? }
? ? // 二進(jìn)制數(shù)據(jù)則直接返回
? ? if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') {
? ? ? return res.data
? ? }
......
})
?完工!