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 ??? */ })
3 个解决方案
#1
39
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上运行测试,这很好。
#2
14
@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 TextDecoder
interface that allows you to decode a string from an ArrayBuffer
(via a DataView
).
现代浏览器(如。FF和Chrome,但还没有IE)现在支持TextDecoder界面,可以从ArrayBuffer(通过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);
}
#3
1
Suppose in your service, you have a function you are using like, This is for Angular 2
假设在你的服务中,你有一个你正在使用的函数,这是角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。希望它能对任何有这个问题的人有所帮助
#1
39
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上运行测试,这很好。
#2
14
@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 TextDecoder
interface that allows you to decode a string from an ArrayBuffer
(via a DataView
).
现代浏览器(如。FF和Chrome,但还没有IE)现在支持TextDecoder界面,可以从ArrayBuffer(通过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);
}
#3
1
Suppose in your service, you have a function you are using like, This is for Angular 2
假设在你的服务中,你有一个你正在使用的函数,这是角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。希望它能对任何有这个问题的人有所帮助