Recently began to learn PHP, there is a problem, PHP in the processing of jquery Ajax back to the JSON data, the Chinese part is empty.
The database is MySQL,
is also the problem of PHP Chinese encoding.
Reference:
(the diagram on the left is resolved before the figure is resolved.)
My Solution code:
1, Back-end PHP (ajaxrequest/getnotifylist.php)
$list =array (' id ' = + $list [id], ' Title ' =>iconv (' gb2312 ', ' UTF-8 ', $list [Title]); If this sentence is not added, there will be an effect on the left, instead, the problem is solved.
<? Header ("Content-type:text/json;charset=utf-8"); require ('.. /common/config.php '); $sql = ' SELECT id,title from ' your_table_name ' ORDER by createdate desc LIMIT 0, 6 ';//echo $sql; mysql_select_db ($db, $conn) or Die ("database". $db. " Connection failed! "); mysql_query ("SET NAMES GBK"); $rs =mysql_query ($sql) or Die (Mysql_error ());/* $result = "", while ($list =mysql_fetch_array ($rs)) {$ID = $list [' ID ']; $Title = $list [' Title ']; $result = $result. $ID. ":". $Title. "|";} if (!empty ($result)) {$result = $result. " Select_success ";} else{$result = "Select_empty";} echo $result; */$notifies =array (); mysql_query ("SET NAMES GBK"); $rs =mysql_query ($sql) or Die (Mysql_error ()) and while ($list =mysql_fetch_array ($rs)) {$list =array (' id ' = + $list [id], ' Title ' =>iconv (' gb2312 ', ' UTF-8 ', $list [Title]); $notifies []= $list; }echo Json_encode ($notifies); Print ($notifies);//print_r ($notifies);?>
2, web front end
Between the head
<script src= "./javascript/jquery-1.8.3.min.js" type= "Text/javascript" charset= "UTF-8" ></script>
<script type= "Text/javascript" >$ (document). Ready (function () {var htmlobj=$.ajax ({url: ' ajaxrequest/ getnotifylist.php ', ' type ': ' Get ', DataType: ' JSON ', success:function (d) {$.each (D,function (index,item) { //output //alert (index); Alert (item.id+ "haha" +item. Title); $ ("#lhScroll"). Append ("<li><a style= ' color:green; ' href= ' # ' val= '" +item.id+ "' >" + item. (Title + "</a></li>");})}); }); </script>
Reference article: http://unixlzx.blog.163.com/blog/static/1027737522011419492160/
End
jquery ajax handling PHP return array, and Json_encode Chinese as empty problem