如何使用DOJO从服务器读取JSON响应?

时间:2011-11-18 11:50:46

标签: javascript json dojo struts2

我在从服务器获取一些数据并用它填充数据存储时遇到了一些麻烦:

这是我在Display.jsp页面中的脚本代码:

dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.data.ItemFileReadStore");

dojo.addOnLoad(function() {

    // our test data store for this example:
    dojo.xhrGet({
        url: "jsonAction.action",// this line can call the action in struts2. I had tested it !
        handleAs: "json",
        preventCache: true,
        load: function(response, ioArgs){
            //dojo.byId("replace").innerHTML = response;
            // I can get json data by changing handleAs:"text"
        //return response; //   
        }
    });

    var  jsonStore = new dojo.data.ItemFileReadStore({
        //??? how can I get the json data?
    });

我想使用这个数据来填充dojox.grid.DataGrid

我正在使用Struts2,Hibernate,Tomcat 6

1 个答案:

答案 0 :(得分:3)

不要使用dojo.xhrGetdojo.data.ItemFileReadStore有一个参数url,您可以在其中指定数据源。这是一个示例网格:

var sampleGrid = new dojox.grid.DataGrid({
    store: new dojo.data.ItemFileReadStore({
        url: "JSON_source",
        clearOnClose: true,
        urlPreventCache: true
    }),
    structure: [ 
        {cells: 
            [[ 
                {field: "JSON_field", name: "displayed value"},
                ...
            ]] 
        }                   
    ],
    ...
});
相关问题