2

I am trying to populate a combobox in ExtJS4 from the struts2 response. But the values are not populated and the combobox is empty. I also tried hard-coding the json data but still it does not work. When I try to append the actionName to the URL and execute the action, there is no error and I can see the json data..

    "{'rows': [{'id': '1','name': 'Google' }, {'id': '2','name': 'Microsoft' }, {'id': '3','name': 'Yahoo' }]} "

I have a form panel, within the form panel I have a combobox item like this:

{
  name: 'bName',
  xtype: 'combobox',
  displayField: 'name',
  valueField: 'id',
  store: new Ext.data.Store({
      fields: [{id: 'id'}, {name: 'name'}],
      autoLoad: true,
      proxy: {
          type: 'ajax',
          url: 'getBookList',
          reader: {
              type: 'json',
              root: 'rows'
          }
      }
  })
}

Here is my struts.xml. I have used the json-plugin and the parameter tag is used so that the json is sent as it is,

<package name="ELM29" extends="struts-default,json-default">
  <action name="getBookList" class="com.test.elm.action.Data"
      method="getBookList">
    <result type="json">
       <param name="root">data</param>
    </result>
  </action>
</package>

Then I have the Book.java where I have the getter setter for the variables including data as a variable and the getBookList method where I am just assigning value to data and returning SUCCESS.

public String getBookList(){  
    data= "{'rows': [{'id':'1','name':'Google'},{'id':'2','name':'Microsoft'},{'id':'3','name':'Yahoo'}]} ";
    return ActionSupport.SUCCESS;
} 

I also tried using Ext.data.JsonStore but it did not help. The problem is caused because of the store or the combobox? What is wrong in the code?

user777777
  • 228
  • 4
  • 25

1 Answers1

0

Your JSON is not valid.

To make it valid, replace all ' (single quote) occurrence with a " (double quote, then escaped: \")

From this great answer:

The formal JSON format does not allow strings in single quotes, but an implementation is allowed to accept them.

So with some libraries will work, with some others it won't.

Now we know ExtJS is one of the latter.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thanks, I changed the json but no change in the output... the combo is not getting populated in the form. But, using form.submit()..I was able to get the json in the result object. – user777777 Nov 17 '14 at 08:29
  • Are you sure your `url: 'getBookList'` doesn't need an extension ? – Andrea Ligios Nov 17 '14 at 12:49
  • Yea pretty sure,because ExtJS and Struts is working fine together. And today when I was trying the same code, I realized that the problem is with loading the store..which I am doing for the first time..and not able to find what exactly is goin wrong :( – user777777 Nov 17 '14 at 13:27
  • mmok, feel free to open a new question with the (old) new problem, that is probably not extJs related – Andrea Ligios Nov 17 '14 at 13:41