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
{ "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 ??? */ })
Edit: As @Paul LeBeau points out, my answer assumes that the response is ASCII encoded.
Basically you just need to decode the ArrayBuffer into a string and use JSON.parse().
I ran tests in IE11 & Chrome and this works just fine.
Suppose in your service, you have a function you are using like, This is for Angular 2
Make sure when you return it it is res.json() and not res.json. Hope it helps, to anyone having this issue
@smkanadl's answer assumes that the response is ASCII. If your response is in another encoding, then that won't work.
Modern browsers (eg. FF and Chrome, but not IE yet) now support the
TextDecoder
interface that allows you to decode a string from anArrayBuffer
(via aDataView
).