From edfabb3b22e39650100430239c2ed17088a6f2b7 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 3 Sep 2012 05:27:27 -0400 Subject: [PATCH] added ability to parse json when gettin json data --- client.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/client.js b/client.js index f0650b83..e2a011a5 100644 --- a/client.js +++ b/client.js @@ -156,7 +156,26 @@ CloudClient.Utils = (function(){ if (lXMLHTTP.readyState === 4 /* Complete */ && lXMLHTTP.status === 200 /* OK */){ var lJqXHR = pEvent.target; - lSuccess_f(lJqXHR.response, lJqXHR.statusText, lJqXHR); + var lData = lJqXHR.response; + + /* If it's json - parse it as json */ + var lContentType = lXMLHTTP.getResponseHeader('content-type'); + if(lContentType && + lContentType.indexOf('application/json') === 0){ + try{ + lData = JSON.parse(lJqXHR.response); + } + catch(pError) { + /* if could not parse */ + console.log('Error: could not parse' + + 'json from server from url: ' + + pParams.url); + + lData = lJqXHR.response; + } + } + + lSuccess_f(lData, lJqXHR.statusText, lJqXHR); } }; }