PDA

View Full Version : Ext.ajax and json response ?!



m0ntassar
26 Nov 2007, 10:48 AM
I'm using Ext.Ajax.request to perform some asynchronous server side data processing ..
The service returns a response in a JSON format ..
Here is my code :

Ext.Ajax.request({
url: 'some url',
method: 'GET',
async: true,
success : function(result) {
alert(result.responseText);
ds.reload();
}
});

The returned json looks like :


{
"obj_id":"71",
"attr2":"47740",
"attr3":"0",
"attr4":"gloub",
"attr5":""
}

I want to retrieve the value of the first param (obj_id) in a js var
how could I do this ?!

PS: I have no access to customise the web service response to get only the value I need :s

hendricd
26 Nov 2007, 11:08 AM
success : function(result) {
var ro = Ext.decode(result.responseText);
alert(ro.obj_id);
ds.reload();
}

26 Nov 2007, 11:12 AM
I would suggest using Ext's json decoder vs. a straight eval.

Check out the wiki entry: http://extjs.com/learn/Manual:Core:Ext.Ajax

hendricd
26 Nov 2007, 11:30 AM
Right you are, my previous post is amended. ;)

26 Nov 2007, 11:34 AM
Doug, it's nice to see other folks (volunteers) helping the community :)

m0ntassar
27 Nov 2007, 1:35 AM
Thx guys ;)
I really apreciate the quality of your support :)

brookd
27 Nov 2007, 8:30 PM
Is there a isJson() method or something like that. What if the server returns an error which is not in json format?

JeffHowden
27 Nov 2007, 9:21 PM
Is there a isJson() method or something like that. What if the server returns an error which is not in json format?

You should never permit the server to return a non-JSON response to a request that's expecting JSON, unless the server is genuinely throwing an error in which case the http status code for the response should be in the 400 or 500 range so the failure handler of the Ajax request object is fired.

m0ntassar
28 Nov 2007, 1:20 AM
You should never permit the server to return a non-JSON response to a request that's expecting JSON, unless the server is genuinely throwing an error in which case the http status code for the response should be in the 400 or 500 range so the failure handler of the Ajax request object is fired.

Any way, if ever this should happen, I think it's useful to use failure: in that case ..



Ext.Ajax.request({
url: 'some url',
method: 'GET',
async: true,
success : function(result) {
//waht ever u need to put there
}
failure : function(result) {
alert('Failed to parse request response');
}
});

brookd
28 Nov 2007, 6:55 AM
Hmm, okay, I guess I just need to create a new CFERROR template that returns JSON. I'm just builing ontop of my existing CFAPP which has an HTML CFERROR page now, I guess I simply need to change that response. Thanks Jeff..