PDA

View Full Version : [SOLVED!] Can't get form's JSON Response data field value



JoyfulBobHome
4 Jan 2010, 2:08 PM
I've got a typical form.load() and I can't pull out a field from the JSON response to work with it in JS. I can't figure out what I'm missing??

my .js:

this.load({
root:'data',
...
success: function( response, options ){
var evlDt = response.data.EVLCO;
// var evlDt = eval(response.data.EVLCO); // Also tried this
Ext.isIE7 ? '' : console.info('evlDt: ' + evlDt);


my JSON response:

{ "data": { "ISTNO":"I00025","EVLSQ":10,"SITID":"CM 9069A","ACTCD":"XA7","EVLPO":"","EVLCD":"01/01/2010","INSCT":"MILWAUKEE","INSST":"WI","EVLQ1":"E","EVLQ2":"E","EVLQ3":"E","EVLQ4":"E","EVLQ5":"E","EVLQ6":"E","EVLQ7":"E","EVLQ8":"E","EVLSC":100.00000,"EVLBY":"BOBK","EVLCO":"01/05/2010" } , "success" : "true" }

FF JS error:

response.data is undefined
var evlDt = response.data.EVLCOfield;\r\n

zhegwood
4 Jan 2010, 2:17 PM
Try logging arguments in your success function and see what properties are in your response object. I'm pretty sure it will come back as response.responseText but I'm not sure. I usually just specify a callback function & not success/failure. Also, if it comes back as a string you might have to call Ext.encode(jsonstr) to get the object.

JoyfulBobHome
5 Jan 2010, 5:45 AM
Thanks for the prompt reply!

I tried that and got this JS error:


too much recursion
[Break on this error] }else if(Ext.isArray(o)){\n

Here's my code:

var jsonEncoded = Ext.encode(response)
//Ext.isIE7 ? '' : console.dir('jsonEncoded: ' + jsonEncoded ); // Tried this also
Ext.isIE7 ? '' : console.info('jsonEncoded: ' + jsonEncoded );

I'll keep trying various code.

Is there an exampe of a callback function? I've heard of it, and seen it in ux's but haven't coded my own. Thanks.

JoyfulBobHome
5 Jan 2010, 5:52 AM
I tried this:


var jsonEncoded = Ext.encode(response.responseText)
Ext.isIE7 ? '' : console.info('jsonEncoded: ' + jsonEncoded );


...and got this:

jsonEncoded: null

...JSON response:

{ "data": { "ISTNO":"I00025","EVLSQ":10,"SITID":"CM 9069A","ACTCD":"XA7","EVLPO":"","EVLCD":"01/01/2010","INSCT":"MILWAUKEE","INSST":"WI","EVLQ1":"E","EVLQ2":"E","EVLQ3":"E","EVLQ4":"E","EVLQ5":"E","EVLQ6":"E","EVLQ7":"E","EVLQ8":"E","EVLSC":100.00000,"EVLBY":"BOBK","EVLCO":"01/05/2010" } , "success" : "true" }

Animal
5 Jan 2010, 6:11 AM
What parameters are sent to the success callback?

zhegwood
6 Jan 2010, 11:55 AM
console.info(arguments)


and see what your response looks like in firebug. It might be breaking because you're trying to encode something that's not json. Callback works like this:



this.load({
callback: function(obj,success,response) {
if (success) {
console.log(response);
}
}
});

Animal
6 Jan 2010, 11:57 AM
That was a test.

What parameters are sent to the submit's success callback?

This information is readily available.

JoyfulBobHome
7 Jan 2010, 6:55 AM
console.info(arguments)


and see what your response looks like in firebug. It might be breaking because you're trying to encode something that's not json. Callback works like this:



this.load({
callback: function(obj,success,response) {
if (success) {
console.log(response);
}
}
});


OK, I tried that and there was nothing logged to the console??



this.load({
url:'NSTEVL',
params: {
submit:'RETRIEVE',
process: 'EDIT',
processType: 'DETAIL',
SITIDi: site,
ISTNOi: istno,
EVLSQi: 10
},
root:'data',
waitMsg:'Loading Evaluation',
callback: function(obj,success,response) {
if (success) {
console.log('log: ' + response); // Nothing logged!
console.info('info: ' + response); // Nothing logged here either!
}
},
success: function( response, options ){
var jsonEncoded = Ext.encode(response.responseText)
Ext.isIE7 ? '' : console.info('jsonEncoded: ' + jsonEncoded );

Animal
7 Jan 2010, 7:08 AM
Hello?

I'm giving you the answer!

What does THE DOCUMENTATION say is passed to your callback?

JoyfulBobHome
7 Jan 2010, 11:12 AM
Hello?

I'm giving you the answer!

What does THE DOCUMENTATION say is passed to your callback?

On the API Doc page I looked at Action.Load (http://www.extjs.com/deploy/ext-3.0-rc2/docs/?class=Ext.form.Action.Submit) and Action (http://www.extjs.com/deploy/ext-3.0-rc2/docs/?class=Ext.form.Action) and found only this:


success : Function
The function to call when a valid success return packet is recieved. The function is passed the following parameters:...
The function to call when a valid success return packet is recieved. The function is passed the following parameters:

* form : Ext.form.BasicForm
The form that requested the action
* action : Ext.form.Action
The Action class. The result property of this object may be examined to perform custom postprocessing.


I'm guessing the answer is in the last statement, "The result property of this object may be examined to perform custom postprocessing.", but clicking that gave me no info or syntax.

I also see this which is how my JSON is formatted:

{
success: true,
data: {
clientName: "Fred. Olsen Lines",
portOfLoading: "FXT",
portOfDischarge: "OSL"
}
}

But this doesn't tell me the callback's returning parm, let alone the syntax. Now, I know I'm wrong, but which doc page? I'm assuming it's under Ext.form.

flanders
7 Jan 2010, 11:28 AM
The documentation tells you that your success-handler will be called with two arguments: the first is the form, the second is an action object, containing the properties you're looking for.

Take a good look at the information under BasicForm.doAction (http://www.extjs.com/deploy/dev/docs/output/Ext.form.BasicForm.html#Ext.form.BasicForm-doAction)


success: function(form, action) {
// Here you can access the action.result property you're refering to
}

JoyfulBobHome
7 Jan 2010, 11:57 AM
Thanks! That did it! I figured it out; whew! :D

Here's my code:

success: function( form, action ){
var encodedResult = Ext.encode(action.result) // Entire data object with its fields
var encodedResultDataINSCT = Ext.encode(action.result.data.INSCT) // Pull out a specific field fom the JSON response
Ext.isIE7 ? '' : console.info('encoded result of form data: ' + encodedResult );
Ext.isIE7 ? '' : console.info('encoded result of a field from the form data: ' + encodedResultDataINSCT );


Here's my JSON response:

{ "data": { "ISTNO":"I00025","EVLSQ":10,"SITID":"CM 9069A","ACTCD":"XA7","EVLPO":"","EVLCD":"01/05/2010","INSCT":"MILWAUKEE","INSST":"WI","EVLQ1":"P","EVLQ2":"F","EVLQ3":"P","EVLQ4":"F","EVLQ5":"P","EVLQ6":"F","EVLQ7":"P","EVLQ8":"F","EVLSC":62.20000,"EVLBY":"BOBK","EVLCO":"01/05/2010" } , "success" : "true" }


Here's my console info with my desired data:

encoded result of form data: {"data":{"ISTNO":"I00025","EVLSQ":10,"SITID":"CM 9069A","ACTCD":"XA7","EVLPO":"","EVLCD":"01/05/2010","INSCT":"MILWAUKEE","INSST":"WI","EVLQ1":"P","EVLQ2":"F","EVLQ3":"P","EVLQ4":"F","EVLQ5":"P","EVLQ6":"F","EVLQ7":"P","EVLQ8":"F","EVLSC":62.2,"EVLBY":"BOBK","EVLCO":"01/05/2010"},"success":"true"}ieEditPanel.js (line 583)
encoded result of a field from the form data: "MILWAUKEE"

Thanks everyone for your assistance! I learned a bunch! =D>