Angular(1.5)使用responseType blob处理来自$ http的JSON错误

时间:2016-06-16 08:32:24

标签: angularjs

我使用 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对象,有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

您无法获得成功和错误案例的不同内容类型,但您可以在单个位置管理转化。写responseError interceptor,您可以在其中检查error.data是否为instanceof Blob并将其转换为JSON