PDA

View Full Version : JSON question - beginner



alex1001
7 Apr 2010, 9:05 AM
Hi,

I need clarification/advice on the following...

I am returing the following from a server:



{success: true, data:{ppid: 789}}


I want to access the ppid value in the following function. I have the following code:



success: function (f, a) {

var ppid = Ext.util.JSON.decode(f.responseText).ppid;

Ext.Msg.alert('RETURN TEST ', ppid);

}



I am getting the following error:


'Ext.util.JSON.decode(...).ppid' is null or not an object


Is my return from the server valid JSON?


If it is valid, then what do I need to do to access the value of ppid ?


Thanks,


Alex.

GoneIn20Seconds
7 Apr 2010, 9:13 AM
I've never used the ExtJs method for decoding JSON so that is new to me, but using Firebug to evaluate the structure of your decoded JSON string it looks like you need to do this to get out your ppid:



var ppid = Ext.util.JSON.decode(str).data.ppid;


Replace "str" of course with your JSON string.

Air_Mike
7 Apr 2010, 9:15 AM
in this example ppid is property of data object, so try this:


success: function (f, a) {
var response = Ext.decode(f.responseText);

Ext.Msg.alert('RETURN TEST ', response.data.ppid);
}

alex1001
7 Apr 2010, 9:31 AM
I tried that, it didnt work.

Any other way to hget the value of ppid ?

Im assuming the JSON string is correct?

GoneIn20Seconds
7 Apr 2010, 9:42 AM
As far as I can tell the JSON string is correct.

Here is the code I am using to test this and it does work for me. Also, I'm using Firefox and running this code directly from the Firebug console.



var str = "{success: true, data:{ppid: 789}}";
var response = Ext.util.JSON.decode(str).data.ppid;
console.info(response);


Also, if this isn't working, why not wrap a try catch statement around your code and output any thrown exceptions?

alex1001
7 Apr 2010, 10:08 AM
Hi, Yes you are right, that does work.

But when I use it in the context I am trying, ie within the function, it doesnt work. I assumed the f parameter would be a handle to the JSON object.

Any ideas ?

GoneIn20Seconds
7 Apr 2010, 10:12 AM
In your function you have:



success: function (f, a) {
var ppid = Ext.util.JSON.decode(f.responseText).ppid;
Ext.Msg.alert('RETURN TEST ', ppid);
}


I'm assuming that "success" is an event listener. If so what type of component are you using that runs that listener?

Maybe show some more of your code?

alex1001
7 Apr 2010, 10:31 AM
I have omitted code to keep it short...




var pageButtons = new Ext.Panel({
items: [{
buttonAlign: 'center',
buttons: [{
text: 'Previous',
handler: function () {
//some code...
Ext.getCmp('myWin').doLayout();
}
},
{
text: 'Next',
handler: function () {
testForm.getForm().submit({
success: function (f, a) {
var ppid = Ext.util.JSON.decode(f).data.ppid;
Ext.Msg.alert('RETURN TEST ', ppid);
},
failure: function (f, a) {
//some code...
}
});
Ext.getCmp('myWin').doLayout();
}
}]
}]
});

alex1001
7 Apr 2010, 10:32 AM
var pageButtons = new Ext.Panel({
items: [{
buttonAlign: 'center',
buttons: [{
text: 'Previous',
handler: function () {
//some code...
Ext.getCmp('myWin').doLayout();
}
},
{
text: 'Next',
handler: function () {
testForm.getForm().submit({
success: function (f, a) {
var ppid = Ext.util.JSON.decode(f).data.ppid;
Ext.Msg.alert('RETURN TEST ', ppid);
},
failure: function (f, a) {
//some code...
}
});
Ext.getCmp('myWin').doLayout();
}
}]
}]
});

karieanis
7 Apr 2010, 2:41 PM
You're performing a submit action on an instance of Ext.form.BasicForm. The success handler will have the form itself, and the instance of Ext.form.Action which handled the request.

You can access the raw response data in the response property of the instance of Ext.form.Action.



{
text: 'Next',
handler: function()
{
testForm.getForm().submit(
{
success: function (f, a)
{
var raw = a.response, rawDecoded = Ext.decode(raw);

Ext.MessageBox.alert('RETURN TEST', rawDecoded.data.ppid);
},
failure: function (f, a)
{
//some code...
}
});

// note that this is going to execute prior to the result of the form submission
Ext.getCmp('myWin').doLayout();
}
}

alex1001
7 Apr 2010, 5:50 PM
You're performing a submit action on an instance of Ext.form.BasicForm. The success handler will have the form itself, and the instance of Ext.form.Action which handled the request.

You can access the raw response data in the response property of the instance of Ext.form.Action.



{
text: 'Next',
handler: function()
{
testForm.getForm().submit(
{
success: function (f, a)
{
var raw = a.response, rawDecoded = Ext.decode(raw);

Ext.MessageBox.alert('RETURN TEST', rawDecoded.data.ppid);
},
failure: function (f, a)
{
//some code...
}
});

// note that this is going to execute prior to the result of the form submission
Ext.getCmp('myWin').doLayout();
}
}



That doesnt work for me. Any other ideas ?