Simple Json connection not working

安稳与你 提交于 2019-12-13 04:33:22

问题


I am trying to familiarise myself with the kendo ui dataSource, I have never before used JSON and have constructed the following using your documentation:

Html:

<!doctype html>
<html>
    <head>
        <title>Kendo UI Web</title>
        <link href="styles/kendo.common.min.css" rel="stylesheet" />
        <link href="styles/kendo.default.min.css" rel="stylesheet" />
        <script src="js/jquery.min.js"></script>
        <script src="js/kendo.web.min.js"></script>
    </head>
    <body>
        <div id="grid"></div>
        <script>
        $(document).ready(function () {
            var myDataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "people.json",
                        dataType: "json"
                    }
                }
            });

            $("#grid").kendoGrid({
                dataSource: myDataSource,
                    columns: [
                    {
                        field: "firstName",
                        title: "First Name"
                    },
                    {
                        field: "lastName",
                        title: "Last Name"
                }]
            });

        });

    </script>
    </body>
</html>

Json File:

{
    "people": [
        { "firstName":"John" , "lastName":"Doe" },
        { "firstName":"Anna" , "lastName":"Smith" },
        { "firstName":"Peter" , "lastName":"Jones" }
    ]
}

The grid doesn't load any data, all I see are the headers.

I have tried this using simply HTML and a JSON file, I have also tried this in Visual Studio.

Any help would be appreciated


回答1:


In your JSON the data transmitted is not an array but an object so you need to specify where in that object the data is stored, making possible to KendoUI DataSource finding it. This is specified using schema.data

Your datasource should be:

var myDataSource = new kendo.data.DataSource({
    transport: {
        read: {
            url     : "people.json",
            dataType: "json"
        }
    },
    schema   : {
        data: "people"
    }
});


来源:https://stackoverflow.com/questions/18979484/simple-json-connection-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!