0

Kendo UI - 教程:使用 Kendo UI 和 PHP 构建应用程序简介 - 第 1 部分中,我了解了如何连接网格。
问题是当 PHP 文件被读取时,它被读取为 HTML 而不是 JSON。解决方法是放置标题。建议的修复是在回显之前在 PHP 文件中指定类型为 JSON。通过添加如下所示的标题:

// add the header line to specify that the content type is JSON
header("Content-type: application/json");

echo "{\"data\":" .json_encode($arr). "}";

我严格遵循该建议,但收到以下错误消息:

警告:无法修改标头信息 - 第 16 行 /home/saulo/public_html/aw001/index.php 中的标头已由(输出开始于 /home/saulo/public_html/aw001/index.php:6)发送

{"data":[{"first_name":"Richard","last_name":"Gere"},{"first_name":"George","last_name":"Clooney"},{"first_name":"Glenda" ,"last_name":"Jackson"},{"first_name":"Lawrence","last_name":"Smith"} 等等...

请帮忙!

我搜索了stackoverflow,但我找不到同样的问题。

4

3 回答 3

1

index.php在调用header函数之前,您已经在第 16 行输出了一些内容。你不能这样做。header必须在进行任何输出之前使用该函数。

检查第 16 行。它很容易只是空行。你需要删除它。

于 2013-04-23T08:01:09.617 回答
0

这是因为在设置标题信息之前,您正在打印一些东西,正如错误所说:

 headers already sent by 
于 2013-04-23T07:44:03.163 回答
-1

谢谢。我把它修好了。它现在正在将类型转换为 application/json。我的 Chrome 控制台中没有显示错误。但仍然没有数据显示。所有显示的是标题。

index.php 如下:

<!DOCTYPE html>
<!-- aw001 wire up grid --> 
<html> 
    <head> 

        <link href="css/kendo.common.min.css" rel="stylesheet">
        <link href="css/kendo.metro.min.css"rel="stylesheet">
        <script src="js/jquery.min.js"></script>
        <script src="js/kendo.all.min.js"></script>
    </head>
    <body>
        <div id="grid"></div>
        <script>
            $(function() {
                $("#grid").kendoGrid({
                    dataSource: {
                        transport: {
                            read: 
                                "data/archers.php"                                
                        },
                        schema: {
                            data: "data"
                        }
                    },
                    columns: [{ field: "FirstName" }, { field: "LastName" }]
                });
            });
        </script> 
    </body> 
</html>

下面的archers.php:

<?php

     $link = mysql_pconnect("localhost", "saulo_admin", "admin") or die("Unable To Connect To Database Server");
     mysql_select_db("saulo_asn") or die("Unable To Connect To ASN");

     $arr = array();
     $rs = mysql_query("SELECT first_name, last_name FROM archers");

     while($obj = mysql_fetch_object($rs)) {
            $arr[] = $obj;
     }
     header("Content-type: application/json");
     echo "{\"data\":" .json_encode($arr). "}";

?>
于 2013-04-23T22:27:24.477 回答