Commit c9a8b708 by ZhengJie

[代码优化](v2.6):修复 responseType 为 blob 时无法解析后台返回的错误信息

close https://github.com/elunez/eladmin/issues/524
parent e310229f
......@@ -22,8 +22,6 @@ service.interceptors.request.use(
return config
},
error => {
// Do something with request error
console.log(error) // for debug
Promise.reject(error)
}
)
......@@ -31,17 +29,21 @@ service.interceptors.request.use(
// response 拦截器
service.interceptors.response.use(
response => {
const code = response.status
if (code < 200 || code > 300) {
Notification.error({
title: response.message
})
return Promise.reject('error')
} else {
return response.data
}
},
error => {
// 兼容blob下载出错json提示
if (error.response.data instanceof Blob && error.response.data.type.toLowerCase().indexOf('json') !== -1) {
const reader = new FileReader()
reader.readAsText(error.response.data, 'utf-8')
reader.onload = function(e) {
const errorMsg = JSON.parse(reader.result).message
Notification.error({
title: errorMsg,
duration: 5000
})
}
} else {
let code = 0
try {
code = error.response.data.status
......@@ -54,6 +56,7 @@ service.interceptors.response.use(
return Promise.reject(error)
}
}
console.log(code)
if (code) {
if (code === 401) {
store.dispatch('LogOut').then(() => {
......@@ -78,6 +81,7 @@ service.interceptors.response.use(
duration: 5000
})
}
}
return Promise.reject(error)
}
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment