不能改变JSON数据(PHP文件)的格式JSON使用的例子在剑道教程


Cannot change format of JSON data (PHP file) to JSON using example in Kendo Tutorial

在《剑道UI -教程:介绍使用剑道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). "}";

我严格遵循了建议,但我得到了以下错误信息:

警告:无法修改标头信息-标头已经在/home/saulo/public_html/aw001/index.php中发送(output started at/home/public_html/aw001/index.php:6)在第16行

{"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,但是没有找到相同的问题

在调用header函数之前,您已经在index.php的第16行上输出了一些东西。你不能那样做。header功能必须在任何输出之前使用。

检查第16行它很容易只有空行。你需要去掉这个

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

 headers already sent by 

谢谢。我把它修好了。它现在正在将类型转换为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). "}";
?>