【问题标题】:Ext.data.JsonStore getCount() returns 0 records web serviceExt.data.JsonStore getCount() 返回 0 条记录 web 服务
【发布时间】:2011-08-05 20:55:15
【问题描述】:

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

以下代码适用于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;

    }
}

【问题讨论】:

    标签: .net web-services sencha-touch extjs jsonstore


    【解决方案1】:

    这对我有用,我单独注册了模型,但使用了数据类型。我使用带有 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();
    

    【讨论】:

    • 感谢您的回复。我添加了您建议的更改,但仍然从 myStore.getCount() 获得 0。我会更新问题以反映您的建议和我一直在尝试的一些事情。
    • 当我遇到这些问题时,我会使用 sencha 的可调试、非缩小、注释版本。在这种情况下,搜索 JsonStore,并在读取方法中设置一些断点,单步执行,可能会看到您的配置中有什么不正确的地方。
    • 这成功了!我必须重新添加标题“Content-Type”,因为我的 Web 服务必须正确返回数据。
    猜你喜欢
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    • 2019-06-11
    • 2021-06-10
    相关资源
    最近更新 更多