【描述】:axios在處理文件下載時(shí)設(shè)置了responseType:blob,期望下載失敗時(shí)在前端報(bào)后端返回的錯(cuò)誤,但此時(shí)后端返回的錯(cuò)誤信息Json已經(jīng)被轉(zhuǎn)為blob對(duì)象,請(qǐng)問如何將blob對(duì)象轉(zhuǎn)回原來(lái)的Json呢?或者說(shuō)怎么處理攔截,因?yàn)閎lob對(duì)象時(shí)axios攔截器無(wú)法判斷該進(jìn)then還是catch。而后端返回非200的錯(cuò)誤狀態(tài)碼時(shí)又不會(huì)返回Json對(duì)象解析【相關(guān)代碼】// axios配置export function exportResult(params) { return noRejectRequest({ url: ..., method: ..., responseType: 'blob', // 轉(zhuǎn)換為Blob對(duì)象而不是DOMString
params
})
}// api請(qǐng)求exportResult({...})
.then(res => {
... // 成功處理})
.catch(err => {
... // 失敗處理,此時(shí)后端會(huì)返回Json格式錯(cuò)誤})【相似問題】axios 的responseType 類型動(dòng)態(tài)設(shè)置 - Celia的回答 - SegmentFault 思否https://segmentfault.com/q/10...
處理文件下載失敗,如何將Blob對(duì)象轉(zhuǎn)換為Json?
繁星點(diǎn)點(diǎn)滴滴
2018-11-16 08:07:57