如何修复JSON.parse:jTable插件的意外字符Line 550 jQuery.js错误?

时间:2013-06-25 14:35:25

标签: javascript jquery html json error-handling

您好我使用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中,数据对象将整个文件作为字符串网络化。我对web dev很新,所以它可能是显而易见的。如果重要的话,我也将它作为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

1 个答案:

答案 0 :(得分:0)

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