View Full Version : [SOLVED] How to process JSON returned from form submit
Troy Wolf
7 Sep 2007, 11:47 AM
Now this one I truly expect to get flamed with RTFM and pointed to a few dozen code examples and relevant threads that I should have found on my own...BUT...I'm NOT finding them. I've spent TOO much time looking.
When I submit() an Ext form, and my server script returns a JSON response, how do I retrieve that JSON response and work with it? Is it naturally received by some object or some trigger that fires when it arrives? (I'm sure the answer is "yes", but what?)
I can see in Firebug that my browser is receiving my JSON just fine, but how to make Ext react to that response?
Believe me, I'm embarrassed to be typing this. Programming for 12 years and authored 1000's of javascript functions and yet banging my head to do the Ext basics. Maybe in 6 months I'll be in here answering posts instead of asking all the questions.
dantheman
7 Sep 2007, 1:12 PM
If it's a form response, your json should look something like (empasis on something :) ):
{ success: true, data: [{ someField: 'someValue' ... etc }, { anotherObject: etc }] }
You can access the data member directly from the second arg of the success handler in form.submit().
Alternately, if you use Ext.Ajax.request, you will have to eval the first arg to the success handler.
That eval will yield a JS object. You also have much more leeway as to how your json is structured.
FWIW, I totally sympathise with the frustration.
Every day is an adventure :D
--dan
jsakalos
8 Sep 2007, 4:54 PM
If you define success and failure form callbacks they are called with two arguments: form and action. You can find returned json somewhere inside of the action object; browse the action object with Firebug to find out exactly.
Troy Wolf
10 Sep 2007, 6:00 AM
So I figured out that my "success" handler function is passed 2 arguments and that I can find the JSON in arguments[1].response.responseText. I can easily access this data with
var myData = eval( "(" + arguments[1].response.responseText + ")" );
I could be happy and move on, but....
I notice that if my JSON does not include a "success" key with the value of true, my success handler is not called. This indicates that Ext has already eval'd my JSON. It seems inefficient, therefore, for me to re-eval my JSON--especially if the JSON object is large. So is there some object through which I can access the already eval'd JSON object?
Animal
10 Sep 2007, 6:32 AM
action.result
Troy Wolf
10 Sep 2007, 6:38 AM
:"> Not sure how I missed that one. Oops. Thanks, Animal...again.
Animal
10 Sep 2007, 10:12 AM
It's easy to miss.
I've decided I just about know my way around the Form and Action classes to begin documenting them.
The docs won't be visible until the next release, and even then, I may have made errors in the syntax of the comments which might make them a bit garbled, but they're on their way.
Troy Wolf
10 Sep 2007, 10:22 AM
I've decided I just about know my way around the Form and Action classes to begin documenting them.
The docs won't be visible until the next release, and even then, I may have made errors in the syntax of the comments which might make them a bit garbled, but they're on their way.
Have I mentioned that I love you? (In a politically correct, non-threatening or harassing way of course.)
Animal
10 Sep 2007, 10:52 AM
:"> :))
I've just comitted the changes to Ext.form.Action. They should be helpful. I'll get onto the Form classes tomorrow.
Powered by vBulletin® Version 4.1.5 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.