【问题标题】:Issues reading from json (kendo ui)从 json 读取的问题 (kendo ui)
【发布时间】:2016-01-13 08:24:11
【问题描述】:

我的老板告诉我要测试 kendo ui,尤其是从 json 读取的调度程序。出于测试目的,我为 json 输出编写了一个 php 文件:

<?php
header("Content-Type:application/json");

$cars = array
    (
        0 => array(
                                'Id'=>'DN-DP-1596',
                                'Title'=>'Business Trip to Cologne',
                                'Description'=>'Appoinment with Acme Inc.',
                                'StartTimeZone'=>null,
                                'Start'=>'2016/1/12 07:00 AM',
                                'End'=>'2016/1/12 09:00 AM',
                                'EndTimeZone'=>null,
                                'RecurrenceRule'=>null,
                                'RecurrenceID'=>null,
                                'RecurrenceException'=>null,
                                'IsAllDay'=>false
            ),
        1 => array(
                                'Id'=>'DN-UQ-1503',
                                'Title'=>'Business Trip to Cologne',
                                'Description'=>'Appoinment with Bauer GmbH.',
                                'StartTimeZone'=>null,
                                'Start'=>'2016/1/12 09:30 AM',
                                'End'=>'2016/1/12 10:30 AM',
                                'EndTimeZone'=>null,
                                'RecurrenceRule'=>null,
                                'RecurrenceID'=>null,
                                'RecurrenceException'=>null,
                                'IsAllDay'=>false
            ),
        2 => array(
                                'Id'=>'DN-VH-791',
                                'Title'=>'Business Trip to Cologne',
                                'Description'=>'Trading conference',
                                'StartTimeZone'=>null,
                                'Start'=>'2016/1/12 01:00 PM',
                                'End'=>'2016/1/12 03:00 PM',
                                'EndTimeZone'=>null,
                                'RecurrenceRule'=>null,
                                'RecurrenceID'=>null,
                                'RecurrenceException'=>null,
                                'IsAllDay'=>false
            )
    );
$callback = $_GET['callback'];
print $callback.'('.json_encode($cars).')';

然后我写了一些html来测试剑道调度器:

    <div id="scheduler"></div>
<script>
    $("#scheduler").kendoScheduler({ date: new Date("2016/1/13") });
    var scheduler = $("#scheduler").data("kendoScheduler");
    var dataSource = new kendo.data.SchedulerDataSource
    ({
        transport:
        {
          read:
          {
              url:"carService.php",
              dataType:"jsonp"                  
          }
        }

    });
    dataSource.fetch(function()
    {
        var cars = dataSource.data();
        var dataItem = dataSource.get(0);

        console.log(dataItem.Title);
    })

不幸的是,我没有得到控制台输出或其他任何东西,对此有什么想法吗?非常感谢您的帮助,我的老板正在恐吓我!

【问题讨论】:

    标签: php json kendo-ui


    【解决方案1】:

    我尝试使用相同的代码并对代码进行了细微的更改。看看下面的代码。

    test.html

    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/styles/kendo.common.min.css"/>
    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/styles/kendo.rtl.min.css"/>
    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/styles/kendo.silver.min.css"/>
    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/styles/kendo.mobile.all.min.css"/>
    
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://kendo.cdn.telerik.com/2016.1.112/js/kendo.all.min.js"></script>
    
    <div id="scheduler"></div>
    <script>
        var dataSource = new kendo.data.SchedulerDataSource({
            transport: {
                read: {
                    url: "test.php",
                    dataType: "jsonp"
                }
            }
        });
        dataSource.fetch(function () {
            var event = this.at(0);
            console.log(event.Title);
        });
    </script>
    

    test.php - 用于 json 数据

    <?php
    $cars = array
    (
        0 => array(
            'Id'=>'DN-DP-1596',
            'Title'=>'Business Trip to Cologne',
            'Description'=>'Appoinment with Acme Inc.',
            'StartTimeZone'=>null,
            'Start'=>'2016/1/12 07:00 AM',
            'End'=>'2016/1/12 09:00 AM',
            'EndTimeZone'=>null,
            'RecurrenceRule'=>null,
            'RecurrenceID'=>null,
            'RecurrenceException'=>null,
            'IsAllDay'=>false
        ),
        1 => array(
            'Id'=>'DN-UQ-1503',
            'Title'=>'Business Trip to Cologne',
            'Description'=>'Appoinment with Bauer GmbH.',
            'StartTimeZone'=>null,
            'Start'=>'2016/1/12 09:30 AM',
            'End'=>'2016/1/12 10:30 AM',
            'EndTimeZone'=>null,
            'RecurrenceRule'=>null,
            'RecurrenceID'=>null,
            'RecurrenceException'=>null,
            'IsAllDay'=>false
        ),
        2 => array(
            'Id'=>'DN-VH-791',
            'Title'=>'Business Trip to Cologne',
            'Description'=>'Trading conference',
            'StartTimeZone'=>null,
            'Start'=>'2016/1/12 01:00 PM',
            'End'=>'2016/1/12 03:00 PM',
            'EndTimeZone'=>null,
            'RecurrenceRule'=>null,
            'RecurrenceID'=>null,
            'RecurrenceException'=>null,
            'IsAllDay'=>false
        )
    );
    $callback = $_GET['callback'];
    print $callback.'('.json_encode($cars).')';
    

    控制台输出:

    Business Trip to Cologne
    

    因此,在回显 json 输出时,您需要在 php 文件中进行所有更改。

    【讨论】:

    • 到目前为止,谢谢你,但是当我像这样更改 php 代码时,我不再得到 json 输出。相反,似乎有一些html标签的东西。
    • 我认为你是对的,但我不知道如何使用它。我从 kendo 观看了一些示例 json,它们以 callback(...) 开头,我做错了什么?
    • json 的输出是什么?
    • 去掉header("Content-Type:application/json");后能不能试试
    • 如果我删除 header()-line 我得到一个未定义的索引异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多