将Kendo UI网格绑定到CRUD REST API

时间:2018-12-20 18:04:14

标签: rest

我想将我通过PHP创建并在本地运行而没有问题的Restful CRUD API绑定到Kendo UI网格,但是不幸的是,没有数据显示在网格中。

我有以下代码来显示公司的部门

<head>
    <title>Διαχείριση  Επαφών</title>
    <link rel="stylesheet" type="text/css" href="lib/kendo.common.min.css">
    <link rel="stylesheet" type="text/css" href="lib/kendo.default.min.css">
    <script src="lib/jquery.min.js"></script>
    <script src="lib/kendo.all.min.js"></script>
    <script src="lib/kendo.web.js"></script>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />

</head>

<body>
    <div id="contacts"></div>

    <script type="text/javascript">

        $(document).ready(function(){


            var remoteDataSource = new kendo.data.DataSource({

                    autoSync: true,

                    transport: {
                                    read    : {
                                    url     : 'http://localhost/contact/api/read.php',
                                    dataType: 'jsonp',
                                    type: 'GET'
                                }
                            },

                    schema: {
                        model: {
                            fields: {
                                id: { type: "number" },
                                name: { type: "string" }
                            }
                        },
                        data: function (response) {
                           return response.Data;
                       }
                    }

                 });



           $("#contacts").kendoGrid({

               dataSource:remoteDataSource,
               pageable: true,
               columns: [
                {field:"id", title: "Department ID", width: "20px"},
                {field: "name", title: "Department", width: "60px"}
               ]
           });



        });

    </script>

</body>

我的宁静API通过PHP中的"Content-Type: application/json; charset=UTF-8"返回以下json数据

{"records":[{"id":"1","name":"SALES"},{"id":"2","name":"STAFF"}]}

有什么想法可以帮助我为什么不将数据完全显示到网格中?

致谢

1 个答案:

答案 0 :(得分:0)

尝试一下:

dataType:“ json”和 json数据必须是这样的:[{“ id”:“ 1”,“ name”:“ SALES”},{“ id”:“ 2”,“ name”:“ STAFF”}]