Browse Source

Adds JSON decode to onFailure #2713

pull/2717/head
jlukic 9 years ago
parent
commit
03e4965571
1 changed files with 16 additions and 10 deletions
  1. 26
      src/definitions/behaviors/api.js

26
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: { read: {
cachedResponse: function(url) { cachedResponse: function(url) {
var var
@ -126,15 +140,7 @@ $.api = $.fn.api = function(parameters) {
} }
response = sessionStorage.getItem(url); response = sessionStorage.getItem(url);
module.debug('Using cached response', url, response); 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; return false;
} }
}, },
@ -516,7 +522,7 @@ $.api = $.fn.api = function(parameters) {
var var
// pull response from xhr if available // pull response from xhr if available
response = $.isPlainObject(xhr) response = $.isPlainObject(xhr)
? (xhr.responseText)
? module.decode.json(xhr.responseText)
: false, : false,
errorMessage = ($.isPlainObject(response) && response.error !== undefined) errorMessage = ($.isPlainObject(response) && response.error !== undefined)
? response.error // use json error message ? response.error // use json error message

Loading…
Cancel
Save