Ext.data.JsonStore getCount() 返回 0 条记录 Web 服务
我正在开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这对我有用,我单独注册了模型,但使用了数据类型。我使用带有 JsonReader 的商店,我删除了您添加的一些额外内容。希望这对您有用......
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...