jquery ajax handling PHP return array, and Json_encode Chinese as empty problem

Source: Internet
Author: User
Tags learn php

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.