2017-07-14 142 views
2

我试图从名为Users的MySQL表中填充jqGrid加载数据。 JS脚本是这样的:parsererror:SyntaxError:JSON.parse:JSON数据的第2行第1列的意外字符200 OK

jQuery脚本

$(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_grid_load_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); 

的jqGrid的加载,但它的中间显示消息:

parsererror: SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data 200 OK {"page":"1","total":1,"records":"1","rows":[{"id":"4","cell":["4","Alexandre","Araujo","alexaraujo73","2"]}]} 

我可以看到从MySQL加载的注册t有能力的用户,但我陷入了这个错误。 任何人都可以帮助我吗?我非常感谢任何帮助。 预先感谢您。

+0

“总”:1失踪“S也许 – uTeisT

+1

** WARNING **:当使用'mysqli'你应该使用[参数化查询(HTTP: //php.net/manual/en/mysqli.quickstart.prepared-statements.php)和['bind_param'](http://php.net/manual/en/mysqli-stmt.bind-param.php)到添加用户数据到您的查询**不要**使用字符串插值或连接来实现这一点,因为您创建了严重的[SQL注入漏洞](http://bobby-tables.com/)**从不**将'$ _POST','$ _GET'或**任意**用户数据直接放入查询中,如果有人试图利用您的错误,可能会造成很大的危害。 – tadman

+0

''''cell'之前''''''''' $ responce-> rows [$ i]'cell'] =' – Andreas

回答

1

这可能是因为任何变量的未定义错误。从你的代码是当你在发送JSON数据来创建

$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 
} 

命名为$responcewhile loop您使用的是以前没有定义的属性rows,所以尽量使用像之前声明它的响应变量, jqgrid,所以你的回应必须是json而不是别的。为了防止响应添加使用error_reporting()像其他人一样的错误,

// hide all notice/warnings 
error_reporting(0); 
+0

_“为了防止错误...”,因为如果我没有看到错误,那么将不会有错误... – Andreas

+0

这只是针对生产版本和开发版本,他可以启用它。 –

+0

我试过了你的提示,但还是得到了同样的错误@Rohan Kumar – Alexandre1973

相关问题