1

您好我正在使用 php 连接到我的数据库。这是我的列表操作代码:表和数据库确实具有相同的名称

<?php
try
{
    //Open database connection
    $con = mysql_connect("localhost","root","");
    mysql_select_db("702data", $con);

    //Getting records (listAction)
    if($_GET["action"] == "list")
    {
        //Get records from database
        $result = mysql_query("SELECT * FROM 702data;");

        //Add all records to an array
        $rows = array();
        while($row = mysql_fetch_assoc($result))
        {
            $rows[] = $row;
        }

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['Records'] = $rows;
        print json_encode($jTableResult);
    }

这是我的 jtable 初始化:

<body>
        <div id="DeviceTableContainer"></div>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#DeviceTableContainer').jtable({
                    title: 'Wireless Monitor',
                    actions: {
                        listAction: 'DeviceActions.php?action=list',
                        createAction: 'DeviceActions.php?action=create',
                        updateAction: 'DeviceActions.php?action=update',
                        deleteAction: 'DeviceActions.php?action=delete'
                    },
                    fields: {
                        DeviceId: {
                            key: true,
                            list: false
                        },
                        DeviceTag: {
                            title: 'Device Tag',
                            width: '40%'
                        },
                        PV: {
                            title: 'PV',
                            width: '10%'
                        },
                        SV: {
                            title: 'SV',
                            width: '10%'
                        },
                        Timestamp: {
                            title: 'Timestamp',
                            width: '30%',
                            type: 'date',
                            create: false,
                            edit: false
                        }
                    }
                });
                $('#DeviceTableContainer').jtable('load');
            });

        </script>
    </body>

我收到的错误在 jQuery.js 的这一行:

parseJSON: function( data ) {
// Attempt to parse using the native JSON parser first
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );////////////////////////////////////
}

看起来它正在解析整个 deviceactions.php 文件,因为在 firebug 中,数据对象将整个文件网络作为字符串连接到它。我对网络开发很陌生,所以这可能是显而易见的。如果这很重要,我也在将其开发为 java web 应用程序。谢谢

编辑:这是有关错误的更多信息

parseJSON()jquery.js (line 550)
data = "<?php\r\ntry\r\n{\r\n //Open ...TableResult);\r\n}\r\n \r\n?>"
ajaxConvert()jquery.js (line 8447)

s = Object { url="DeviceActions.php?action=list", type="POST", isLocal=false, more...}

response = "<?php\r\ntry\r\n{\r\n //Open ...TableResult);\r\n}\r\n \r\n?>"

jqXHR = Object { readyState=4, responseText="<?php\r\ntry\r\n{\r\n //Open ...TableResult);\r\n}\r\n \r\n?>", getResponseHeader=function(), more...}

isSuccess = true
4

1 回答 1

0

确保您的服务器 (PHP) 在返回响应时将内容类型标记为 JSON

于 2013-06-25T14:43:41.937 回答