fix: 功能修改完善。
This commit is contained in:
@@ -31,7 +31,16 @@ const upload = <T>(config: RequestUploadConfig): Promise<ResponseData<T>> => {
|
||||
header: config.header,
|
||||
formData: config.formData,
|
||||
success: (res) => {
|
||||
let result = JSON.parse(res.data)
|
||||
let result
|
||||
// 后端可能返回 JSON 字符串(正常)或 HTML 错误页(如 413 Payload Too Large)
|
||||
// 必须包 try-catch,否则 JSON.parse 招错后 Promise 永远不 settle,mask loading 永不关闭
|
||||
try {
|
||||
result = JSON.parse(res.data)
|
||||
} catch (e) {
|
||||
toast('上传失败:服务器响应非 JSON 格式(可能文件过大)')
|
||||
reject('上传失败:响应解析失败')
|
||||
return
|
||||
}
|
||||
const code = result.code || 200
|
||||
// @ts-ignore
|
||||
const msg = errorCode[code] || result.msg || errorCode['default']
|
||||
|
||||
Reference in New Issue
Block a user