0

我为这个问题搜索了一段时间,但找不到太多关于它的信息。

在我的 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 方法,我初始化了我的服务“解决方法”,我的 jsonreststore 可以使用它来处理授权。

在萤火虫(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

会不会是跨域问题?我知道我的后端服务支持跨域。

4

1 回答 1

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();
});
于 2013-04-09T13:40:43.430 回答