2011-02-02 58 views
1

我試圖強制ExtJS和Java之間的通信我正在從ExtJS發送請求到使用netty的Java服務器。如果有人能夠給我一個例子,說明如何從java方面編寫響應,以及如何從ExtJS方面提前讀取響應數據,我會感到滿意。 這是從ExtJS的側ExtJS JsonStore和Netty

var store = new Ext.data.JsonStore({ 
    autoload: true, 
    baseParams: { 
     conid : '6b09477f-aa04-4f5a-b969-05277d01f07e' 
    }, 
    root: 'OpenCashTime', 
    proxy: new Ext.data.ScriptTagProxy({ 
     url: 'http://localhost:8080/getOpenCash?' 
    }), 
    fields: [{name: 'Time', mapping: 'Time', type: 'int'}]     
}); 
store.load(); 
store.on('load', function() { 
    alert(store.getTotalCount());     
}); 
store.on('write', function() {     
    alert(store.getTotalCount()); 
}); 
store.on('loadexception', function() { 
    alert("AAAAAA"); 
}); 
store.on('metachange', function() { 
    //alert(store.getTotalCount()); 
}); 
store.on('update', function() { 
    //alert(store.getTotalCount()); 
}); 
store.on('save', function() { 
    //alert(store.getTotalCount()); 
}); 
store.on('datachanged', function() { 
    //alert(store.getTotalCount()); 
}); 

我的源代碼在執行這個代碼,並reciving這個響應{ 「OpenCashTime」:[{ 「時代」:1291623637000},{ 「時代」:1294914317000}]}我仍然得到即使螢火蟲看到它的Json

+0

爲什麼選擇ScripTagProxy?你會從跨域來源加載數據嗎? – Mchl 2011-02-02 17:32:30

+0

是的,現在我正在測試本地主機加上它不在同一臺服務器上運行的Apache Tomcat服務器上的ExtJS和netty部分運行在不同的端口,因此它被視爲跨域 – Swine1973 2011-02-02 17:35:51

+1

好吧。對於ScriptTagProxy,您的響應必須如下所示:`callback({「OpenCashTime」:[{「Time」:1291623637000},{「Time」:1294914317000}]})`。 – Mchl 2011-02-02 17:40:36

回答

2

假設你的標題,你想要加載數據到JsonStore,它期望一個有效的Json字符串,一個屬性存儲一個JSON對象數組,將作爲記錄加載。配置JsonStore時,屬性名稱由root屬性設置。

商店這樣的:

{ 
    xtype: 'jsonstore', 
    root: 'data', 
    idProperty: 'ID', 
    fields: [ 
    {name: 'ID', mapping: 'ID', type: 'int'} 
    {name: 'someDate', mapping: 'someDate', type: 'date', dateFormat: 'Y-m-d'} 
    ], 
    url: 'hereBeYourURl' 
} 

會很高興地吃了這樣的事情:

{"data":[{"ID":"1","someDate":"2002-10-02"},{"ID":"2","someDate":"2002-05-22"}]} 
1
fields: [{name: 'Time', mapping: 'Time', type: 'int'}] 
fields: [{name: 'Time', type: 'int'}] 

BTW標識映射的情況下,你可以離開它。這兩種情況會給你 的結果相同。

相關問題