我正在使用Sencha Touch应用程序,无法从我的Web服务获取数据。
以下代码适用于第{4页} sencha's forums上的其他用户。我已修改它以匹配我的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;
}
}
答案 0 :(得分:0)
这对我有用,我分别注册了模型,但是有数据类型。我使用带有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();