parsererror:SyntaxError:JSON.parse:JSON数据的第2行第1列的意外字符200 OK

时间:2017-07-14 06:19:31

标签: php jquery mysql mysqli

我正在尝试从名为Users的MySQL表中填充jqGrid加载数据。 js脚本如下所示:

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表用户加载的寄存器,但我遇到了这个错误。 有谁能够帮我?我非常感谢任何帮助。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

可能是因为任何变量的未定义错误。从您的代码中创建一个名为$responce的响应变量,并在while loop中使用之前未定义的属性rows,因此请在使用之前尝试声明它,

$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而不是其他内容。为了防止在响应中添加其他错误,请使用error_reporting()之类的,

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