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