0

I am using ExtJs 3.4 and I've implemented an Ajax request as following.

Ext.Ajax.request({
                      method: 'GET',
                      loadMask: true,
                      scope: this,
                      url: "http://" + host + ":" + port + "/" + projectName + "/"
                            + "SetDefaultValues",
                      success: function (response, request) {
                        Ext.MessageBox.alert('success', response.responseText);
                        var jsonData = Ext.util.JSON.decode(response.responseText);
                        console.log(jsonData);

                        Ext.Msg.alert('Status', String(jsonData.name));
                      },
                      failure: function (response, request) {
                        Ext.MessageBox.alert('failure', response.responseText);
                      },
                      params : {
                        selectedVCode : selVehicleCode,
                        selectedFromDate : selFromDate.format('Y-m-d')
                      }
                 });

My Ajax response is like this.

{'defaultVal':[{ 'category' : '1', 'name' : 'Kamal Subhasingha' } , { 'category' : '2', 'name' : 'Namal Witharana' } , { 'category' : '3', 'name' : 'Yohan' } , { 'category' : '4', 'name' : 'Ahan Liyanage' } , { 'category' : '5', 'name' : 'Sampath Jayaweera' } , { 'category' : '6', 'name' : 'Saman' } ]}

I try to retrieve the fields from this data set. Here is my attempt.

success: function (response, request) {
                        Ext.MessageBox.alert('success', response.responseText);
                        var jsonData = Ext.util.JSON.decode(response.responseText);
                        console.log(jsonData);

                        Ext.Msg.alert('Status', String(jsonData.name));
                      }

This prints jsonData set in the console. It's ok. But it gives an alert with 'undefined'. It says 'String(jsonData.name)' is undefined.

What's going wrong with my codes.

Please help me to clarify this issue.

Rose18
  • 2,892
  • 8
  • 47
  • 98
  • 2
    Well, the object doesn't have a property `name`. It has a property `defaultVal`, which is an array of objects, each having a property `name`. – Felix Kling Jan 13 '14 at 07:30
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Jan 13 '14 at 07:30

0 Answers0