1
您好我正在使用PHP連接到我的數據庫。這裏是我的名單動作代碼:表和數據庫做有相同的名字我該如何修復JSON.parse:意外字符行550 jQuery.js jTable插件的錯誤?
<?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
你能在Firebug檢查請求(標誌內容類型爲JSON或類似)併發布返回的JSON。 –
最有可能你的JSON無效。一個JSON響應應該只包含JSON,沒有html,沒有額外的字符串或空格等。 –
add console.log(data);到您發佈的jquery.js代碼的第二行,以便我們可以看到響應 – reekogi