开发者

Ext.data.JsonStore getCount() returns 0 records web service

开发者 https://www.devze.com 2023-02-25 10:03 出处:网络
I am working on a Sencha Touch application, and can\'t get data from my web service. The following code worked for another user on sencha\'s forums,page 4.I\'ve modified it to match my web services o

I am working on a Sencha Touch application, and can't get data from my web service.

The following code worked for another user on sencha's forums,page 4. I've modified it to match my web services output json.

var myStore = new Ext.data.JsonStore({
        id: 'Agents',
        proxy: new Ext.data.HttpProxy({
            url: 'ws/Service.asmx/GetAgents'
            ,method: 'post'
            ,jsonData: {}
            ,headers: { 'Content-Type': 'application/json; charset=utf-8;'}
            ,reader:{root:'d', record:'rows'}
        }),
        totalProperty: 'd.totalRows',
        idProperty: '开发者_C百科AgentID',
        fields: ['AgentID', 'FirstName','LastName'],
        autoLoad:'true',
        listeners: {
                beforeload: function(myStore, options) {
                    console.log('beforeload: myStore.count = ' + myStore.getCount());
                    console.log(options);
                },
                load: function(myStore, records, options) {
                    console.log('load: ' + myStore.getCount());
                    console.log(records)
                    console.log(options);
                },
                exception: function(misc) {
                    console.log('exception:');
                    console.log(misc);
                }
            }
});

Firebug Console Output:

beforeload: myStore.count = 0
load: 0
[]
true

Firebug confirms the JSON returned from 'ws/Service.asmx/GetAgents' is:

{"d":{"success":true,"totalRows":2,"rows":[{"AgentID":1,"FirstName":"Jelena","LastName":"Akerhus"},{"AgentID":2,"FirstName":"Londo","LastName":"Molari"}]}}

However, when I type 'myStore.getCount()' into the console I get 0 records.

Here is the part of the code for Service.asmx:

[System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true, XmlSerializeString = false)]
    public object GetAgents()
    {
        List<Agent> agents = new List<Agent>();
        agents.Add( new Agent(1, "Jelena", "Akerhus") );
        agents.Add( new Agent(2, "Londo", "Molari") );

        object data = new { success = true, totalRows = agents.Count, rows = agents };
        return data;

    }
}


This worked for me, I registered the model separately, but with data types. I use a store with a JsonReader, I removed some of the extra things you've added in. Hope this works for you...

      Ext.regModel('Agent', {
        idProperty:'AgentID',
        fields: [{name:'AgentID', type:'int'},{name:'FirstName', type:'string'},{name:'LastName', type:'string'}]
      });

      var store = new Ext.data.Store({
        model  : 'Agent',
        proxy: 
        {
           type:'ajax',
           url:'test.json',
           reader:{
             type:'json',
             root:'d.rows',
             totalProperty: 'd.totalRows'
           }
        },
        autoLoad:'true'
      });

      var list = new Ext.List({
        fullscreen: true,
        itemTpl : '{FirstName} {LastName}',
        store: store
      });
      list.show();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号