Search code examples
extjsextjs4extjs3

Returning string as json in extJs


I am working on extJs 3.2.x

I have a simple function which returns a string.

public Object getRevenueCurrency(....) {
    return "USD";
} 

I have Jackson mapper to map the response type to JSON.

<bean name="jsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
        <property name="contentType">
            <!-- <value>application/json</value> -->
            <value>text/html</value>
        </property>
    </bean> 

An attempt to retrieve the data goes like this:

currencyStore = new Ext.data.JsonStore({
id:'currencystore',
url: 'xxxxxxx?action=getcurrency',
root: 'string',         
listeners: {load: function(store) {
       rev_currency=store.?????;
    }
}
});
currencyStore.on('exception',function( store, records, options ){
alert('Exception was called');
},this);

Fiddler shows a response from server as :

{"string": "USD"}

Although I get no server or js exception,the exception alert is called.

1.How do i extract the currency value ?
2.What is a way to extract a meaningful information on the exception in the exception handler above?


Solution

  • I notice you have not configured any fields on your store, so when the reader takes your json respsonse its trying to find a field named 'string' to bind to.

    Take a look at the ExtJS3 docs for the JsonStore -> http://docs.sencha.com/extjs/3.4.0/#!/api/Ext.data.JsonStore

    The sample config shows you how the fields are defined, so your store should look like:

    currencyStore = new Ext.data.JsonStore({
    id:'currencystore',
    url: 'xxxxxxx?action=getcurrency',
    fields: ['string'],         
    listeners: {load: function(store, records, options) {
           rev_currency=store.?????;
        }
    }
    });
    

    Note I have also changed the params to your load function handler, so the new record created from your json can be accessed using records[0]