From e1315d27a4f5c7e3177e79631f4a1fcd4f148626 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 29 Oct 2015 17:52:18 +1030 Subject: [PATCH] Only parse as JSON if appropriate content type --- js/lib/App.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/js/lib/App.js b/js/lib/App.js index 01360f61f..b75523df2 100644 --- a/js/lib/App.js +++ b/js/lib/App.js @@ -202,13 +202,7 @@ export default class App { // When we deserialize JSON data, if for some reason the server has provided // a dud response, we don't want the application to crash. We'll show an // error message to the user instead. - options.deserialize = options.deserialize || (responseText => { - try { - return JSON.parse(responseText); - } catch (e) { - throw new RequestError(500, responseText, options); - } - }); + options.deserialize = options.deserialize || (responseText => responseText); options.errorHandler = options.errorHandler || (error => { throw error; @@ -233,7 +227,13 @@ export default class App { throw new RequestError(status, responseText, options, xhr); } - return responseText; + if (xhr.getResponseHeader('content-type').indexOf('json') !== -1) { + try { + return JSON.parse(responseText); + } catch (e) { + throw new RequestError(500, responseText, options, xhr); + } + } }; if (this.requestError) this.alerts.dismiss(this.requestError.alert);