【问题标题】:parsererror: SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data 200 OK解析器错误:SyntaxError:JSON.parse:JSON 数据的第 2 行第 1 列出现意外字符 200 OK
【发布时间】:2017-12-19 03:26:07
【问题描述】:

我正在尝试从名为 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 表 users 加载的寄存器,但我陷入了这个错误。 有谁能够帮我?我真的很感激任何帮助。 提前谢谢你。

【问题讨论】:

  • "total": 1 可能缺少"s?
  • 警告:使用mysqli 时,您应该使用parameterized queriesbind_param 将用户数据添加到您的查询中。 请勿使用字符串插值或连接来完成此操作,因为您创建了严重的SQL injection bug切勿$_POST$_GET任何用户数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。
  • 'cell'] 之前缺少[$responce-&gt;rows[$i]'cell']=
  • 您可能会发出两个 JSON 文档而不是一个。
  • @Andreas 这是一个错字,否则他将无法从服务器端获取 JSON 数据。

标签: php jquery mysql mysqli


【解决方案1】:

这可能是因为任何变量的未定义错误。从您的代码中,您正在创建一个名为 $responce 的响应变量,而在 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
}

当您向 jqgrid 发送 JSON 数据时,因此您的响应必须是 json 并且没有其他内容。为了防止在响应中添加其他错误,请使用 error_reporting() 之类的,

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

【讨论】:

  • “为了防止错误...” 因为如果我没有看到错误,那么就不会有错误...
  • 这只是生产版本,开发版本他可以启用它。
  • 我已经尝试了你的提示,但仍然遇到同样的错误@Rohan Kumar
猜你喜欢
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2019-11-12
  • 2020-03-25
相关资源
最近更新 更多