Page 1 of 3

Manipulate REST output before JSON parsing

Posted: Sun Jan 06, 2013 6:24 am
by Renjith V

Am stuck with a problem. In one of the REST calls, the server seems to be returning "None" if there is not data to be returned. I agree that the data being returned in not JSON formatted and the correction has to be in the server. But is there a way that I can handle this in the app, to change "None" to empty before the JSON parsing is done?


Manipulate REST output before JSON parsing

Posted: Sun Jan 06, 2013 12:42 pm
by Renjith V

Anyone out there please :(


Manipulate REST output before JSON parsing

Posted: Sun Jan 06, 2013 2:41 pm
by Emmz

I use this...
For The Rest Service COMPLETE event add..
code
var responseTxt =(jqXHR.responseText);
var responseTxt = JSON.parse(responseTxt);
console.log(responseTxt.response);//see the object
if (ParsedResponse == 'None'){
//do this
}
/code

As I need to check for returned data of servers output b4 Acting..


Manipulate REST output before JSON parsing

Posted: Sun Jan 06, 2013 6:03 pm
by Renjith V

Hey Neil, thank you, it's a relief to know that it can be done. But I am still a bit lost. My aim is to set back a valid JSON into the actual data so that when the default parsing happens it's gets a valid one.
Could you please help me out on how to handle that?

In my case the parse function throws an error. See the stack below:

Uncaught SyntaxError: Unexpected token N jquery-1.8.2.js:514
jQuery.extend.parseJSON jquery-1.8.2.js:514
Tiggzi.DataSource.$t.createClass.__responseDataHandler tiggzi.js:1207
proxy jquery-1.8.2.js:775
fire jquery-1.8.2.js:974
self.fireWith jquery-1.8.2.js:1082
done jquery-1.8.2.js:7788
callback


Manipulate REST output before JSON parsing

Posted: Sun Jan 06, 2013 6:27 pm
by Renjith V

I just tried to add a console.log to know whether those functions (Succes/Error/Complete) are being called. I guess, none of them are even called. Nothing came out on the console expect for the above mentioned exception :(


Manipulate REST output before JSON parsing

Posted: Sun Jan 06, 2013 6:43 pm
by maxkatz

Just start with this and go from there:

code
var responseTxt =(jqXHR.responseText);
console.log(responseTxt);
/code


Manipulate REST output before JSON parsing

Posted: Sun Jan 06, 2013 6:45 pm
by Renjith V

I did exactly the same. But the callback functions (none of them are being called).
And nothing comes in console, just the exception that I pasted above.


Manipulate REST output before JSON parsing

Posted: Sun Jan 06, 2013 6:49 pm
by maxkatz

Add 'error' callback to see what's happening.


Manipulate REST output before JSON parsing

Posted: Sun Jan 06, 2013 6:57 pm
by Renjith V

Adding it as a Reply to add an image.


Manipulate REST output before JSON parsing

Posted: Sun Jan 06, 2013 6:58 pm
by Renjith V

No, 'error' call back also is not called.

I also tried this. Instead of using the Invoke service method, I tried to use 'Run Javascript' and tried to invoke the service like serviceInstanceName.execute({}); within a try/catch block, hoping that I can catch this error.
Why is that? Because the error output clearly says an uncaught exception :(
Image