我使用 responseType:blob 调用 $ http 时遇到了问题。 我想在JSON中处理错误响应,但即使我在服务器上将响应的内容类型设置为'application / json',我也会得到blob数据。
$http.post(url, data, {responseType: 'blob'})
.then(function(response) {
// download the file (works fine)
})
.catch(function(error) {
// handle JSON response error
});
如果不尝试convert error data对象,有没有办法做到这一点?
答案 0 :(得分:1)
您无法获得成功和错误案例的不同内容类型,但您可以在单个位置管理转化。写responseError
interceptor,您可以在其中检查error.data
是否为instanceof Blob
并将其转换为JSON