收到响应后,AngularJS可以设置responseType吗? [英] Can AngularJS set the responseType after a response has been received?

查看:449
本文介绍了收到响应后,AngularJS可以设置responseType吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Angular 1.x应用程序,希望使用$http.post()调用接收二进制文件下载(pdf).问题是,我想替代地获得作为json发送的处理错误消息.我可以使用config

I have an Angular 1.x application that is expecting to receive a binary file download (pdf) using a $http.post() call. The problem is, I'd like to alternatively get a processing error message that's sent as json. I can do this with the config

headers: {
  'Accept': 'application/pdf, application/json'
}

问题是我必须设置responseType: 'arraybuffer',否则pdf二进制文件将被转义(或更改为不加载).但是,这会导致无法正确读取或解释json.

The problem is I have have to set responseType: 'arraybuffer', otherwise the pdf binary is escaped (or altered such that it doesn't load). However, that prevents the json from being read or interpreted correctly.

我怎么都可以呢?

编辑:我将尝试澄清;也许我的理解不正确.

I'm going to try to clarify; perhaps my understanding is incorrect.

$http({
    method: 'POST',
    url: "/myresource",
    headers: {
        'Accept': 'application/pdf, application/json'
    },
    responseType: 'arraybuffer'
})
.then(
    function(response) {
        // handle pdf download via `new Blob([data])`
    }, function(response) {
        // pop up a message based on response.data
    }
)

在我返回pdf数据块和http状态为200的情况下,第一个函数处理响应并提示用户保存文件.但是,如果状态为错误(422),则response.data未定义.我认为这是因为responseType阻止了json的正确处理.

In a scenario where I return a pdf data block and a http status of 200, the first function handles the response and prompts the user to save the file. However if the status is an error (422), the response.data is undefined. I assume this is because the responseType is preventing the json from being handled correctly.

如果我删除了responseType行,则可以正确读取错误数据,但是当保存pdf时,某些文件字节不正确,并且实际上已损坏.我认为这是因为文件被编码是因为javascript期待一个字符串.

If I remove the responseType line, the error data is correctly read, but when the pdf is saved, some of the file bytes aren't correct and it's effectively corrupted. I assume this is because the file is being encoded because javascript was expecting a string.

推荐答案

加载响应后,无法更改XHR responseType属性.但是arraybuffer可以根据Content-Type进行解码和解析:

An XHR responseType property can not be changed after a response has been loaded. But an arraybuffer can be decoded and parsed depending on Content-Type:

 var config = {
    responseType: "arraybuffer",
    transformResponse: jsonBufferToObject,
  };

  function jsonBufferToObject (data, headersGetter, status) {
      var type = headersGetter("Content-Type");
      if (!type.startsWith("application/json")) {
        return data;
      };
      var decoder = new TextDecoder("utf-8");
      var domString = decoder.decode(data);
      var json = JSON.parse(domString);
      return json;
  };

  $http.get(url, config);

上面的示例将XHR设置为返回arraybuffer,并使用transformResponse函数检测Content-Type: application/json并在必要时进行转换.

The above example sets the XHR to return an arraybuffer and uses a transformResponse function to detect Content-Type: application/json and convert it if necessary.

在PLNKR上进行演示

这篇关于收到响应后,AngularJS可以设置responseType吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆