javascript 如果 responseType 是 arraybuffer,如何从 $http 读取 JSON 错误响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30052567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:35:07  来源:igfitidea点击:

How to read JSON error response from $http if responseType is arraybuffer

javascriptangularjsangular-http

提问by hansmaad

I load some binary data using

我使用加载一些二进制数据

$http.post(url, data, { responseType: "arraybuffer" }).success(
            function (data) { /*  */ });

In case of an error, the server responds with an error JSON object like

如果出现错误,服务器会响应一个错误 JSON 对象,例如

{ "message" : "something went wrong!" }

Is there any way to get the error response in a different type than a success response?

有什么方法可以获得与成功响应不同类型的错误响应?

$http.post(url, data, { responseType: "arraybuffer" })
  .success(function (data) { /*  */ })
  .error(function (data) { /* how to access data.message ??? */ })

回答by smkanadl

Edit: As @Paul LeBeau points out, my answer assumes that the response is ASCII encoded.

编辑:正如@Paul LeBeau 指出的那样,我的回答假设响应是 ASCII 编码的。

Basically you just need to decode the ArrayBuffer into a string and use JSON.parse().

基本上你只需要将 ArrayBuffer 解码成一个字符串并使用 JSON.parse()。

var decodedString = String.fromCharCode.apply(null, new Uint8Array(data));
var obj = JSON.parse(decodedString);
var message = obj['message'];

I ran tests in IE11 & Chrome and this works just fine.

我在 IE11 和 Chrome 中进行了测试,效果很好。

回答by Paul LeBeau

@smkanadl's answer assumes that the response is ASCII. If your response is in another encoding, then that won't work.

@smkanadl 的回答假定响应是 ASCII。如果您的响应采用另一种编码,那么这将不起作用。

Modern browsers (eg. FF and Chrome, but not IE yet) now support the TextDecoderinterface that allows you to decode a string from an ArrayBuffer(via a DataView).

现代浏览器(例如 FF 和 Chrome,但尚不支持 IE)现在支持TextDecoder允许您从ArrayBuffer(通过 a DataView)解码字符串的接口。

if ('TextDecoder' in window) {
  // Decode as UTF-8
  var dataView = new DataView(data);
  var decoder = new TextDecoder('utf8');
  var response = JSON.parse(decoder.decode(dataView));
} else {
  // Fallback decode as ASCII
  var decodedString = String.fromCharCode.apply(null, new Uint8Array(data));
  var response = JSON.parse(decodedString);
}

回答by Adeel Imran

Suppose in your service, you have a function you are using like, This is for Angular 2

假设在您的服务中,您有一个正在使用的功能,例如,这是用于 Angular 2

someFunc (params) {
    let url = 'YOUR API LINK';
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    headers.append('Authorization','Bearer ******');
    return this._http
            .post(url, JSON.stringify(body), { headers: headers})
            .map(res => res.json());    
}

Make sure when you return it it is res.json() and not res.json. Hope it helps, to anyone having this issue

确保返回时它是 res.json() 而不是 res.json。希望对遇到此问题的人有所帮助