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

How can I read a JSON reponse from server using DOJO?

本文关键字:JSON 响应 读取 服务器 何使用 DOJO      更新时间:2023-09-26

我有一些麻烦从服务器获取一些数据,并用它填充数据存储:

这是我在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

不使用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"},
                ...
            ]] 
        }                   
    ],
    ...
});