You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

jQuery jqGrid升级PHP7后JSON位置0出现意外字符I报错如何排查

jQuery jqGrid 升级PHP 7.x后JSON解析报错处理记录

问题背景

站点从低版本PHP升级到PHP 7.x后,原本可正常运行的PHP+jQuery jqGrid数据网格维护程序无法正常加载数据,控制台抛出错误:Syntax Error: Unexpected token I in JSON at position 0,长时间无法定位根源。

核心代码

前端jqGrid初始化代码

jQuery("#list2").jqGrid({ url:'?location=locations&action=get',
      datatype: "json",
      colNames:['id','Activity', 'Name','Shortname', 'Active'],
      colModel:[
          {name:'locationid',index:'fas_locationid', width:25,editable:false,editoptions:{size:10}},
          {name:'Activity',index:'abc_activityid', width:90,editable:true,edittype:"select",editoptions:{dataUrl:'?location=sports&action=get&oper=select',value:sports},formatter:'select'},
          {name:'name',index:'name', width:200,editable:true,editoptions:{size:50},editrules:{required:true}},
// 其余配置省略
         ],
      rowNum:40, rowList:[40,80,120],pager: '#pager2',sortname: 'locationid',
      viewrecords: true, height: 600,width:1020,  
      loadComplete: function () {
        alert("OK"); },   
    loadError: function (jqXHR, textStatus, errorThrown) {
        //var obj = jQuery.parseJSON(jqXHR.responseText);
        alert('HTTP status code: ' + jqXHR.status + '\n' +
              'textStatus: ' + textStatus + '\n' +
              'errorThrown: ' + errorThrown);
        alert('HTTP message body (jqXHR.responseText): ' + '\n' + jqXHR.responseText);
    },
      sortorder: "asc",
      editurl:"?location=locations&action=edit",
      caption:"Locations" });

loadError回调返回的报错信息

HTTP status code: 200
textStatus: parseerror
errorThrown: Syntax Error: Unexpected token 'I' in JSON at position 0

后端数据生成代码

$i =0;
foreach ($Locations as $Location) {
 
    $this->response->rows[$i]['id']= $Location->get('locationid');
    $this->response->rows[$i]['cell']=array($Location->get('locationid'),$Location->get('activityid'), /* 其余字段省略 */
);
   $i++; 
}
$jsData = json_encode($this->response);
$ecode = json_last_error_msg();  // 捕获JSON编码错误,返回结果为"No Errors"
echo $jsData;

排查过程

  1. 首先验证JSON编码逻辑:json_encode执行后json_last_error_msg返回无错误,打印$this->response结构符合jqGrid要求的JSON格式,排除后端JSON生成错误。
  2. 查看接口实际响应:通过Chrome开发者工具的网络面板查看接口返回内容,发现响应最开头混入了异常内容:
I<br />
<b>Warning</b>:  Creating default object from empty value in <b>/Applications/MAMP/htdocs/myproj/public_html/manager/controllers/manager.class.php</b> on line <b>2297</b><br />

该内容后才是正常的JSON数据,正是开头的字符I导致JSON解析失败。
3. 进一步排查字符来源:未找到I字符的具体输出逻辑,初步判断疑似jquery.min.js在网格创建过程中写入响应文本导致,最终通过调整业务实现方案规避了该问题。

同类型问题通用解决思路

  • 优先排查PHP错误输出:PHP 7.x对未初始化变量、对象的操作警告级别更高,低版本中不报错的逻辑升级后可能抛出警告混入响应。可临时在接口代码开头添加error_reporting(0)验证,同时修复警告对应的代码逻辑即可解决。
  • 清理输出缓冲区:在输出JSON前添加ob_clean()清空之前所有的输出内容,避免文件BOM头、其他逻辑提前输出的字符混入。
  • 检查代码首尾冗余内容:确认接口对应的PHP文件开头<?php前、结尾?>后没有多余的空格、换行或打印逻辑。
  • 排查前端拦截逻辑:如果确认后端输出无异常,检查是否有全局请求拦截器、JS插件修改了接口响应内容。

内容的提问来源于stack exchange,提问作者SenorMogul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 14:36:04