Php parsererror:SyntaxError:JSON.parse:JSON数据的第2行第1列出现意外字符200 OK
我试图填充一个jqGrid,从名为Users的MySQL表加载数据。 js脚本如下所示: jQUERY脚本Php parsererror:SyntaxError:JSON.parse:JSON数据的第2行第1列出现意外字符200 OK,php,jquery,mysql,mysqli,Php,Jquery,Mysql,Mysqli,我试图填充一个jqGrid,从名为Users的MySQL表加载数据。 js脚本如下所示: jQUERY脚本 $(function () { "use strict"; jQuery("#list2").jqGrid({ url:'users_grid_load_data.php?q=2', datatype: "json", mtype: "GET", colNames:['Id','Firs
$(function () {
"use strict";
jQuery("#list2").jqGrid({
url:'users_grid_load_data.php?q=2',
datatype: "json",
mtype: "GET",
colNames:['Id','First Name', 'Last Name', 'Username','Level'],
colModel:[
{name:'id_user',index:'id_user', width:55},
{name:'firstname',index:'firstname', width:90},
{name:'lastname',index:'lastname', width:90},
{name:'username',index:'username', width:90},
{name:'level',index:'level', width:80, align:"right"}
],
rowNum:10, rowList:[10,20,30],
pager: '#pager2',
sortname: 'id_user',
viewrecords: true,
sortorder: "asc",
height:"auto",
width:"auto",
caption:"LIST OF USERS"
});
jQuery("#list2").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false});
});
现在这是users\u grid\u load\u data.php文件:
$page = $_GET['page'];
$limit = $_GET['rows'];
$sidx = $_GET['sidx'];
$sord = $_GET['sord'];
$result = $mysqli->query("SELECT COUNT(*) AS count FROM users");
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$count = $row['count'];
if( $count > 0 && $limit > 0) {
$total_pages = ceil($count/$limit);
} else {
$total_pages = 0;
}
if ($page > $total_pages) $page=$total_pages;
$start = $limit*$page - $limit;
if($start <0) $start = 0;
$SQL = "SELECT * FROM users ORDER BY $sidx $sord LIMIT $start , $limit";
$result = $mysqli->query( $SQL );
$i=0;
$responce = new stdClass();
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
$responce->rows[$i]['id']=$row['id_user'];
$responce->rows[$i]'cell']=
array($row['id_user'],$row['firstname'],
$row['lastname'],$row['username'],$row['level']);
$i++;
}
echo json_encode($responce);
我可以看到从MySQL表用户加载的寄存器,但我陷入了这个错误。
有人能帮我吗?我真的很感谢你的帮助。
提前谢谢。这可能是因为任何变量的未定义错误。在代码中,您正在创建名为
$response
的响应变量,在while loop
中,您正在使用以前未定义的属性rows
,因此请在使用like之前尝试声明它
$responce = new stdClass();
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$responce->rows=array();// create an array of rows here
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
$arr = array('id'=>$row['id_user'],'cell'=>array($row['id_user'],
$row['firstname'],
$row['lastname'],
$row['username'],
$row['level']) // cell closing
); // closing of arr
$responce->rows[]=$arr; // push it to $rows of $reponce
//$i++; // no need of it
}
当您将JSON数据发送到jqgrid时,您的响应必须是JSON而不是其他。为了防止在响应中添加其他错误,请使用
“总数”:1人失踪“s maybe?警告:当使用
mysqli
时,您应该使用和将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为您已经创建了严重的错误。切勿将$\u POST
、$\u GET
或任何用户数据直接放入查询中,如果有人试图利用您的错误,这可能非常有害。在'cell']之前缺少[/code>:$response->行[$i]“cell']=
您可能正在发送两个JSON文档,而不是一个。@Andreas因为这是一个输入错误,否则他将无法从服务器端获取JSON数据。“为了防止错误…”因为如果我没有看到错误,那么就不会有错误…这只是生产版本和开发版本,他可以启用它。我尝试了你的提示,但仍然得到相同的错误@Rohan Kumar
$responce = new stdClass();
$responce->page = $page;
$responce->total = $total_pages;
$responce->records = $count;
$responce->rows=array();// create an array of rows here
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
$arr = array('id'=>$row['id_user'],'cell'=>array($row['id_user'],
$row['firstname'],
$row['lastname'],
$row['username'],
$row['level']) // cell closing
); // closing of arr
$responce->rows[]=$arr; // push it to $rows of $reponce
//$i++; // no need of it
}
// hide all notice/warnings
error_reporting(0);