Php ajax interaction Chinese character garbled problem solution

Source: Internet
Author: User
Tags character set eval json php tutorial

The data transmitted by the server is still gb2312 encoded. The client uses js to convert Chinese characters into UTF-8 encoded characters and displays them on the page.

Method 1 json
1. iconv function conversion for server json data using php Tutorial: iconv ('gb2312', 'utf8', "converted string, output to the browser ");
The client obtains the utf8 data and converts it to gb2312:

Function gb2utf8 (data) {// gb code is, ie is converted to Chinese through the binary code utf8-> gbk
Var glbencode = [];
Gb2utf8_data = data;
Execscript ("gb2utf8_data = midb (gb2utf8_data, 1)", "vbscript ");
Var t = escape (gb2utf8_data ). replace (/% u/g ,""). replace (/(. {2 })(. {2})/g |>, "% $ 2% $1 "). replace (/% ([a-z].) % (. {2})/g |>, "@ $1 $2 ");
T = t. split ("@");
Var I = 0, j = t. length, k;
While (++ I <j)>
K = t. substring (0, 4 );
If (! Glbencode [k]) {
Gb2utf8_char = eval ("0x" + k );
Execscript ("gb2utf8_char = chr (gb2utf8_char)", "vbscript ");
Glbencode [k] = escape (gb2utf8_char). substring (1, 6 );
         } 
T = glbencode [k] + t. substring (4 );
     } 
Gb2utf8_data = gb2utf8_char = null;
Return unescape (t. join ("% "));
}

2. header ("content-type", "application/x-www-form-urlencoded; charset = gbk"); // Set the output header to gbk encoding

3. Call the preceding method before ajax requests data to specify the character set used by the request: xmlhttp. setrequestheader ("content-type", "application/x-www-form-urlencoded; charset = gbk ");


Solution 2
Search. php

<? Php
Header ("content-type: text/html; charset = gb2312 ");
Include './search.htm ';
?>

Search.htm

<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "content-type" content = "text/html; charset = gb2312"/>
<Title> Advanced Search </title>
</Head>
<Body>
<H3> Advanced Search <Form method = "post" action = "">
School type:
<Select name = "schooltype">
<Option value = ""> All </option>
<Option value = "1"> Primary School </option>
<Option value = "2"> Junior High School </option>
</Select>
School name:
<Select name = "sid" id = "sid">
<Option value = ""> Select a School </option>
</Select>
</Form>
<Script type = "text/webpage effects">
Function ajax (settings ){
Var xhr = window. activexobject? New activexobject ("microsoft. xmlhttp"): new xmlhttprequest (), successed = false;
Xhr. open (settings. type, settings. url );
If (settings. type = 'post ')
Xhr. setrequestheader ('content-type', 'application/x-www-form-urlencoded ');
Xhr. send ((! Settings. cache? 'Time = '+ new date (). gettime () +' & ': '') + settings. data );
Settings. loader ();
Settimeout (function (){
If (! Successed ){
Alert ('resquest timeout! ');
Xhr. abort ();
        }
}, Settings. timeout );
Xhr. onreadystatechange = function (){
If (xhr. readystate = 4 & xhr. status = 200 ){
Settings. callback (xhr. responsetext. replace (/(^ s *) | (s * $)/g ,""));
        }
Successed = true;
    }
}
Function a (t ){
Ajax ({
Type: 'post ',
Url: 'Ajax. Php ',
Data: 'schooltype = '+ t,
Timeout: 8000,
Cache: true,
Loader: function (){},
Callback: function (d ){
Var arr = eval (d );
If (typeof (arr) = 'object '){
Var obj, option;
Document. getelementbyid ('Sid '). innerhtml = '';
For (var I = 0; obj = arr; I ++ ){
Option = document. createelement ('option ');
Option. value = obj [0];
Option. innerhtml = txt2utf8 (obj [1], '& #');
Document. getelementbyid ('Sid '). appendchild (option );
    }
   }
  }
})
}
Function txt2utf8 (string, prefix ){
For (var I = 0, utf8 = []; I <string. length; utf8.push (prefix | 'u') + string. charcodeat (I ++ )));
Return utf8.join ('');
}
A (0 );
</Script>
</Body>
</Html>

Ajax. php
<? Php
Header ("content-type: text/html; charset = gb2312 ");
$ Schooltype =! Empty ($ _ post ['schooltype'])? $ _ Post ['schooltype']: 0;
Switch ($ schooltype ){
Case 0:
Echo "[['40', 'huali Bao Elementary School in Taoxi town '], ['41', 'longling Heilongjiang Hope Primary School in Taoxi town'], ['42 ', 'letanxi Town Junior High School'], ['43 ', 'lianyuan Junior High School'] ";
Break;
Case 1:
Echo "[['40', 'huali Bao Elementary School in Taoxi town '], ['41', 'longling Heilongjiang Hope Primary School in Taoxi town']";
Break;
Case 2:
Echo "[['42', 'letanxi Town Junior High School '], ['43', 'lianyi Junior High School in Lotte Creek ']";
Break;
Default:
Break;
}
?>

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.