Jquery ajax retrieves json data returned by php to generate a select drop-down box

Source: Internet
Author: User
Tags json pack

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.

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.