我正在开发 Sencha Touch 应用程序,但无法从我的 Web 服务中获取数据。
以下代码适用于sencha 论坛上的另一个用户,第 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;
}
}