原因: json_encode($str) 中的字符串 必须是 utf-8的格式:

 

--------------------------------

问题描述:

返回的json数据:

 1 <?php
 2     $array = array();
 3     $rows = $parameter["list"];
 4     $User = $parameter["User"];
 5     $userId = $parameter["userId"];
 6     $data = '';
 7     $data.= '
 8         <table class="table table-striped table-hover">
 9             <tbody>';
10     foreach ($rows["rows"] as $row){
11         $data.= '<tr>
12                 <td  width="40px"><input type="checkbox" name="checkItem" value="'.$row->id.'" /></td>';
13         $data.= '<td width="200px">'.BaseUtil::setEntities($row->title).'</td>';
14                 $User->getDataById($row->userId);
15         $data.= '<td width="200px">'.BaseUtil::setEntities($User->userName).'</td>';
16         $data.= '<td width="200px">'.'<img style="width:100px;" src="'.UPLOAD_HTTP.$row->icon.'"/>'.'</td>';
17         $data.= '<td width="200px">'.BaseUtil::setEntities($row->orderBy).'</td>';
18         $data.= '<td width="200px">'.BaseUtil::setEntities($row->isOpenEnum[$row->isOpen]).'</td>';
19         $data.= '<td width="200px">
20                     <button type="button" class="btn btn-success btn-sm" onclick="base.addTab(\'知识点管理\',\'action.php?c=KnowledgeProxy&a=index&courseId='.$row->id.'\',\'课程管理\')">
21                         <span class="glyphicon glyphicon-pencil" style="color:#fff; cursor:pointer;"></span>&nbsp;
22                         知识点管理
23                     </button>
24                 </td>';
25         $data.= '<td  width="100px">
26                         <span class="table-list-btn glyphicon glyphicon-edit" title="编辑" style="color:#2e6da4; cursor:pointer;" onclick="base.showCourseUpdate('.$row->id.')"></span>&nbsp;
27                         <span class="table-list-btn glyphicon glyphicon-trash" title="删除" style="color:#d43f3a; cursor:pointer;"  onclick="base.removeCourse('.$row->id.')"></span>&nbsp;
28                 </td>
29             </tr>';
30     }
31     $data.= '</tbody></table>';
32     $array ["data"] = $data;
33     $array ["total"] = $rows ["total"];
34     echo BaseUtil::toJson($array);
35 ?>

 

setEntities 方法:
1 /**
2      * 转字符换为实体
3      *
4      * @param unknown $string
5      * @return string
6      */
7     public static function setEntities($string) {
8         return htmlentities ( $string);
9     }

 

出现的问题是 :

1)在公司里面 返回的json数据是正常的 . 例如:

  

2) 但是如果 是在家里 ,就出现错误. data 是null

   {data: null , total:3}

 

 同样的代码 ,只是因为在公司和 家中的php环境不同 就出现了不同的结果.

 

 

-----------

测试如果不使用:BaseUtil::setEntities($User->userName) 处理数据 ,直接用$User->userName 的话,在家中 就可以正常使用了.

 BaseUtil::setEntities 是对函数 htmlentities的封装.

1 /**
2      * 转字符换为实体
3      *
4      * @param unknown $string
5      * @return string
6      */
7     public static function setEntities($string) {
8         return htmlentities ( $string );
9     }

 

查阅php手册中关于 htmlentities的描述:

 

可见,htmlentities 是要设置转换的格式的, 如果没有设置, 默认读取 php配置文件中的 default_charset; 

 同时在 php 5.4 之后  ,默认的转码为 utf-8 .

同时 ,json_encode 要求 字符串必须是utf-8的.

 

-------

所以问题 就是 我公司的电脑的php版本大于 5.4 ,使用 封装的BaseUtil::setEntities 自动转为 utf-8 ; 家中的电脑的php版本 低于5.4

 

----

解决办法:

第一: 将家中电脑的php.ini  的 default_charset 设置为 utf-8 , 但是没有起作用.

第二: 重写 BaseUtil::setEntities ,指定格式 . 此时家中 可以正常使用了.

1 /**
2      * 转字符换为实体
3      *
4      * @param unknown $string
5      * @return string
6      */
7     public static function setEntities($string) {
8         return htmlentities ( $string,  ENT_COMPAT  , 'utf-8' );
9     }