Example
The code is as follows: |
Copy code |
<Script type = "text/javascript" language = "JavaScript"> Function select (){ Var pro_id = $ ("# pro"). val (); $. Ajax ({ 'URL': 'cartage _ response. php? Act = ajaxGetCity & region_id = '+ pro_id, 'Type': 'post ', 'Ype Ype ': 'json ', Success: function (msg ){ $ ("# City" 2.16.html (''); $. Each (msg, function (index, val ){ $ ("# City"). append ('<option value =' + val. region_id + '>' + val. region_name + '</option> '); }) } }); } </Script> |
Json data returned by php
The code is as follows: |
Copy code |
While ($ row = $ db-> fetch_array ($ query )){ $ Row ['uname'] = mb_convert_encoding ($ row ['uname'], 'utf-8', 'gbk '); $ Row ['content'] = mb_convert_encoding ($ row ['content'], 'utf-8', 'gbk '); $ Com [] = $ row; } Echo json_encode ($ com ); |
Using json_encode () to process the array, we need to encode and convert the Chinese characters in the array to the UTF-8.
Note: values retrieved from the UTF-8 database do not need to be converted.
After conversion, the output json format is:
The code is as follows: |
Copy code |
{"Name": "\ u674e \ u662d \ u8c15", "nick": "\ u5929 \ u5802 \ u7693 \ u6708", "contact": {"email ": "zhaoyulee@126.com", "website": "http: \ // www.111cn.net"}, "sex": "\ u7537 "} |
If you want to convert and display Chinese directly, you can do the following:
The code is as follows: |
Copy code |
<? Php Function Notice (){ Include './include/conn. Php'; // Database link file $ SQL _notice = mysql_query ('select * FROM gg_notice where enable = "1" limit 0, 10 '); $ Notice = mysql_fetch_array ($ SQL _notice, MYSQL_ASSOC ); $ Str = json_encode ($ notice ); // Linux Return preg_replace ("#\\\ u ([0-9a-f] {4}) # ie", "iconv ('ucs-2be', 'utf-8 ', pack ('h4 ',' \ 1 ') ", $ str ); // Windows // Return preg_replace ("# \\\ u ([0-9a-f] {4}) # ie", "iconv ('ucs-2le', 'utf-8 ', pack ('h4 ',' \ 1 ') ", $ str ); } ?> |
Well, it's actually mainly about Encoding. If the Chinese character is solved, there will be no Chinese null or null characters.