1
我正在使用Sencha Touch應用程序,並且無法從我的Web服務獲取數據。Ext.data.JsonStore getCount()返回0記錄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);
}
}
});
螢火蟲控制檯輸出:
beforeload: myStore.count = 0
load: 0
[]
true
螢火蟲確認JSON從 'WS/Service.asmx/GetAgents' 返回是:
{"d":{"success":true,"totalRows":2,"rows":[{"AgentID":1,"FirstName":"Jelena","LastName":"Akerhus"},{"AgentID":2,"FirstName":"Londo","LastName":"Molari"}]}}
然而,當I型「的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;
}
}
感謝您的答覆。我添加了你建議的更改,但仍然從myStore.getCount()獲得0。我會更新問題以反映您的建議以及我一直在嘗試的一些內容。 – 2011-04-14 23:21:03
當我遇到這些問題時,我使用sencha的可調試,未縮小的註釋版本。在這種情況下,搜索JsonStore,並在讀取方法中設置一些中斷點,逐步完成並可能看到您的配置中不正確。 – ballmw 2011-04-14 23:44:59
這工作!我不得不添加標題'Content-Type',因爲我的Web服務必須正確返回數據。 – 2011-04-15 13:18:02