Pedro Cabo
Pedro Cabo

Reputation: 93

Getting Data from my Web-Service using proxy:Rest from Sencha Touch 2

While using this code, i can't get any results...

Ext.define('Teste.view.Main', {
extend: 'Ext.Container',
initialize: function(){    
    Ext.define('User', {
        extend: 'Ext.data.Model', 
       config: {  
            fields: ['description', 'discountCode' , 'prodCode'],
            proxy: {
                type: 'rest',
                format:'json',
                url : 'http://localhost:8080/stcws/resources/com.database.productcode/'
            }     
        }    
    });     
    var store = new Ext.create('Ext.data.Store', {     
        model: 'User'  
    }); 
    store.load();
    console.log(store.getCount());   
}});

But if i take out

format:'json',

I get a XML response, what i'm doing wrong?

Upvotes: 0

Views: 3057

Answers (2)

Pedro Cabo
Pedro Cabo

Reputation: 93

To use the 'Rest' proxy and get a Json response we just need to add to the Proxy:

headers: {                
    'Accept' : 'application/json'                 
},

And take out...

format:'json',

Upvotes: 2

shashankaholic
shashankaholic

Reputation: 4122

Use Json Reader to read server response.

http://docs.sencha.com/touch/2-0/#!/api/Ext.data.reader.Json

Try:

proxy: {
        type: 'ajax',
        url : 'users.json',
        reader: {
            type: 'json'
        }
    }

or

 proxy: {
            type: 'ajax',
            url : 'users.json',
            reader: {
                type: 'json'
                model: 'User'
            }
        }

Upvotes: 0

Related Questions