From 03e4965571c19da29f6952de3b99ac52a468453a Mon Sep 17 00:00:00 2001 From: jlukic Date: Thu, 23 Jul 2015 12:22:25 -0400 Subject: [PATCH] Adds JSON decode to onFailure #2713 --- src/definitions/behaviors/api.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/definitions/behaviors/api.js b/src/definitions/behaviors/api.js index 3234e574f..d145a4e1c 100644 --- a/src/definitions/behaviors/api.js +++ b/src/definitions/behaviors/api.js @@ -115,6 +115,20 @@ $.api = $.fn.api = function(parameters) { } }, + decode: { + json: function(response) { + if(response !== undefined && typeof response == 'string') { + try { + response = JSON.parse(response); + } + catch(e) { + // isnt json string + } + } + return response; + } + }, + read: { cachedResponse: function(url) { var @@ -126,15 +140,7 @@ $.api = $.fn.api = function(parameters) { } response = sessionStorage.getItem(url); module.debug('Using cached response', url, response); - if(response !== undefined) { - try { - response = JSON.parse(response); - } - catch(e) { - // didnt store object - } - return response; - } + response = module.decode.json(response); return false; } }, @@ -516,7 +522,7 @@ $.api = $.fn.api = function(parameters) { var // pull response from xhr if available response = $.isPlainObject(xhr) - ? (xhr.responseText) + ? module.decode.json(xhr.responseText) : false, errorMessage = ($.isPlainObject(response) && response.error !== undefined) ? response.error // use json error message