Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: JSON question - beginner

  1. #1

    Default JSON question - beginner

    Hi,

    I need clarification/advice on the following...

    I am returing the following from a server:

    Code:
    {success: true, data:{ppid: 789}}
    
    I want to access the ppid value in the following function. I have the following code:

    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.

  2. #2

    Default Use the data...

    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:

    Code:
    var ppid = Ext.util.JSON.decode(str).data.ppid;
    Replace "str" of course with your JSON string.

  3. #3
    Sencha Premium Member
    Join Date
    Sep 2007
    Posts
    67

    Default

    in this example ppid is property of data object, so try this:

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

  4. #4

    Default

    I tried that, it didnt work.

    Any other way to hget the value of ppid ?

    Im assuming the JSON string is correct?

  5. #5

    Default

    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.

    Code:
    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?

  6. #6

    Default

    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 ?

  7. #7

    Default Not enough info

    In your function you have:

    Code:
    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?

  8. #8

    Default

    I have omitted code to keep it short...

    Code:
     
    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();
                }
            }]
        }]
    });

  9. #9

    Default

    Code:
     
    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();
                }
            }]
        }]
    });

  10. #10
    Sencha User karieanis's Avatar
    Join Date
    Jun 2008
    Location
    Melbourne, Australia
    Posts
    110

    Default

    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.

    Code:
    {
        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(); 
        }
    }
    Jeremy Rayner
    Software Engineer

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •