通过jsonreststore将dojo.grid绑定到restful服务并进行身份验证

时间:2013-04-09 13:01:17

标签: javascript json rest dojo dojox.grid.datagrid

我搜索了一下这个问题,但找不到多少。

在我的javascript应用程序中,我尝试通过jsonreststore和dgrid可视化我的restful后端的数据。

这就是我到目前为止所得到的:

    <script>
        function getRequest(args) {
            return {
                url: 'http://myworkingapiwithevents/events',
                handleAs: 'json',
                sync: false,
                headers: {
                    'Authorization': 'Basic HriB5jsHUib2K='
                }
            }
        }

        require(["dojo/store/JsonRest", "dojo/rpc/JsonService"], function (JsonRest, JsonService) {            
            service = new JsonService('http://myworkingapiwithevents/events', true /*isJson*/, undefined /*schema*/, getRequest);
            myStore = new JsonRest({ service: service });
        });

        require(["dojox/grid/DataGrid", "dojo/data/ObjectStore", "dojo/domReady!"
        ], function (DataGrid, ObjectStore) {
            grid = new DataGrid({
                store: dataStore = new ObjectStore({ objectStore: myStore }),
                structure: [
                    { name: "Event", field: "name", width: "200px" }
                ]
            }, "grid3");
            grid.startup();
        });
    </script>

首先,我使用硬编码的base64授权,该授权适用于我的后端服务。使用getRequest方法,我初始化我的服务“workaround”,我的jsonreststore可以使用它来处理授权。

在firebug(Chrome)中,我收到以下错误:

ErrorCtor {stack: "Error: Unable to load http://myworkingapiwithevents/ev... p://localhost:52894/Scripts/dojo/dojo.js:1094:43)", message: "Unable to load http://myworkingapiwithevents/events status: 0", response: Object, status: 0, responseText: ""…}

Error {popStackFrame: function} "Error: Unable to load SMD from http://myworkingapiwithevents/events

可能是某些跨域问题?我知道我的后端服务支持跨域。

1 个答案:

答案 0 :(得分:2)

您不需要使用dojo / rpc / JsonService。试试这个:

require(["dojo/store/JsonRest", "dojox/grid/DataGrid", "dojo/data/ObjectStore", "dojo/domReady!"], function (JsonRest, DataGrid, ObjectStore) {
        var restStore = new JsonRest({
            target : 'http://myworkingapiwithevents/events',
            headers: {'Authorization': 'Basic HriB5jsHUib2K='}
        });
        var dataStore = new ObjectStore({ objectStore : restStore });
        grid = new DataGrid({
            store: dataStore,
            structure: [
                { name: "Event", field: "name", width: "200px" }
            ]
        }, "grid3");
        grid.startup();
});