Ext.data.JsonStore getCount() 返回 0 条记录 Web 服务

发布于 2024-11-01 15:13:09 字数 2387 浏览 6 评论 0原文

我正在开发 Sencha Touch 应用程序,无法从我的 Web 服务获取数据。

以下代码适用于 sencha's forums,第 4 页。我已对其进行了修改以匹配我的 Web 服务输出 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: '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 控制台输出:

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

Firebug 确认从“ws/Service.asmx/GetAgents”返回的 JSON 为:

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

但是,当我在控制台中输入“myStore.getCount()”时,我得到 0 条记录。

这是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;

    }
}

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: '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;

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

椒妓 2024-11-08 15:13:09

这对我有用,我单独注册了模型,但使用了数据类型。我使用带有 JsonReader 的商店,我删除了您添加的一些额外内容。希望这对您有用......

      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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文