fix(AjaxStream): Parse json response on error

This commit is contained in:
Berkeley Martinez
2018-01-29 11:22:39 -08:00
parent 040d49d612
commit 8025f96fa1

View File

@ -64,11 +64,27 @@ function getCORSRequest() {
} }
} }
function parseXhrResponse(responseType, xhr) {
switch (responseType) {
case 'json':
if ('response' in xhr) {
return xhr.responseType ?
xhr.response :
JSON.parse(xhr.response || xhr.responseText || 'null');
} else {
return JSON.parse(xhr.responseText || 'null');
}
case 'xml':
return xhr.responseXML;
case 'text':
default:
return ('response' in xhr) ? xhr.response : xhr.responseText;
}
}
function normalizeAjaxSuccessEvent(e, xhr, settings) { function normalizeAjaxSuccessEvent(e, xhr, settings) {
var response = ('response' in xhr) ? xhr.response : xhr.responseText;
response = settings.responseType === 'json' ? JSON.parse(response) : response;
return { return {
response: response, response: parseXhrResponse(settings.responseType || xhr.responseType, xhr),
status: xhr.status, status: xhr.status,
responseType: xhr.responseType, responseType: xhr.responseType,
xhr: xhr, xhr: xhr,
@ -266,7 +282,8 @@ export function postJSON$(url, body) {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Accept: 'application/json' Accept: 'application/json'
} },
normalizeError: (e, xhr) => parseXhrResponse('json', xhr)
}) })
.map(({ response }) => response); .map(({ response }) => response);
} }
@ -291,6 +308,7 @@ export function getJSON$(url) {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Accept: 'application/json' Accept: 'application/json'
} },
normalizeError: (e, xhr) => parseXhrResponse('json', xhr)
}).map(({ response }) => response); }).map(({ response }) => response);
} }