XMLHttpRequest By default is to pass data with UTF-8. When the server and the client and the database unified use UTF-8 encoding can effectively avoid garbled problems. XMLHttpRequest can also work correctly if the server has the correct Content-type Response header and encoded information.
However, when using XMLHttpRequest to read Chinese Web page content, if the service side of the program is not set Content-type Response header, or the header does not set the encoding type, Then we may encounter garbled characters when we visit the ResponseText property. As the following code with XMLHttpRequest get Code Academy home page:
- xmlhttp = getxmlhttprequest ();
- var url =
- xmlhttp.open ( "GET", url, true);
- xmlhttp.onreadystatechange = function () {
- if (xmlhttp.readystate == 4)
- if (xmlhttp.status == 200)
- alert ( Xmlhttp.responsetext);
- };
- xmlhttp.send (null);
Even though the Code academy this Professional tutorial site, the support for Web standards is not complete, pop-up HTML source is flooded with web-standard HTML tags, and of course, the expected garbled.
also unfortunately, Firefox and IE solution is also a different way
Firefox practice:
Firefox XMLHttpRequest object supports the Overridemimetype method, you can specify the encoding type of the returned data, This method can be used to solve the Chinese garbled, the previous code is modified as follows:
- xmlhttp = getxmlhttprequest ();
- var url = "http://cn.astrology.yahoo.com/";;
- xmlhttp.open ( "GET", url, true);
- xmlhttp.overridemimetype (//setting gb2312 encoding to identify data
- xmlhttp.onreadystatechange = function () {
- if (xmlhttp.readystate == 4)
- if (xmlhttp.status == 200)
- alert (xmlhttp.responsetext);
- }; 
- xmlhttp.send (null);
Internet Explorer Practices:
IE does not support the Overridemimetype method and can only be solved in a very awkward way, when a hybrid function needs to be introduced:
- function Gb2utf8 (data) {
- 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[i].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[i]=glbencode[k]+t[i].substring (4);
- }
- Gb2utf8_data = Gb2utf8_char = null;
- Return unescape (T.join ("%"));
- }
- XMLHTTP = Getxmlhttprequest ();
- var url = "http://www.uxuew.cn/";;
- Xmlhttp.open ("GET", url, true);
- Xmlhttp.onreadystatechange = function () {
- if (xmlhttp.readystate = = 4)
- if (Xmlhttp.status = = 200)
- Alert (Gb2utf8 (xmlhttp.responsebody)); //Note here to use Responsebody
- };
- Xmlhttp.send (null);
The
Gb2utf8 function directly parses the binary data returned by XMLHttpRequest, where the Execscript method is used to execute the function of VBScript. So that's a hybrid function. Thanks for the algorithm provided by the Blueidea forum. The
has a workaround but is ugly and does not conform to web standards. So try to avoid it in programming, if you are developing a Web application, use UTF-8 encoding as much as possible, or set the correct encoding information on the server
Reason: Ajax in receiving responsetext or Responsexml value is in accordance with the UTF-8 format to decode, if the server segment sent data is not UTF-8 format, It is possible that the value of receiving responsetext or responsexml is garbled.
Workaround: Specify the format for sending data on the server:
- In the JSP file:
- Response.setcontenttype ("Text/text;charset=utf-8"); The txt text file is returned
- Or
- Response.setcontenttype ("Text/xml;charset=utf-8"); The returned XML file
- Php:header (' content-type:text/html;charset=gb2312 ');
- ASP:Response.Charset ("GB2312")
- JSP:response.setHeader ("Charset","GB2312");
The complete code is as follows:
- <script>
- var xmlHttp;
- var browertype="ie";
- function Createxml () {
- try{
- XmlHttp = new ActiveXObject ("msxml2.xmlhttp");
- } catch (e) {
- try{
- XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
- }catch (E2) {
- XmlHttp =false;
- }
- }
- if (!xmlhttp && typeof XMLHttpRequest! = ' undefined ') {
- XmlHttp = new XMLHttpRequest ();
- Browertype = "FF"; //used to record whether Firefox is used for processing below to receive Chinese data analysis.
- }
- }
- function Querytelcode (citys) {
- Createxml ();
- Showstate=document.getelementbyid ("Showtelcode");
- Xmlhttp.onreadystatechange = function () {
- if (xmlhttp.readystate = = 2) {
- showstate.innerhtml = ' ';
- }Else if (xmlhttp.readystate = = 4 && Xmlhttp.status = =) {
- if (browertype=="ff") {
- Getlastcode=xmlhttp.responsetext; //firefox
- }else{
- Getlastcode=gb2utf8 (Xmlhttp.responsebody);
- }
- showstate.innerhtml = Getlastcode;
- }
- }
- var url=' http://www.uxuew.cn?oid=<%=Request.QueryString ("OrderID")%>&cityname= ' +citys;
- Xmlhttp.open ("GET", url,true);
- if (browertype=="ff") {
- Xmlhttp.overridemimetype ("text/html;charset=gb2312"); Set to identify data with gb2312 encoding, only FF support. ie not
- }
- Xmlhttp.send (null);
- }
- function Gb2utf8 (data) {
- 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[i].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[i]=glbencode[k]+t[i].substring (4);
- }
- Gb2utf8_data = Gb2utf8_char = null;
- Return unescape (T.join ("%"));
- }
- </script>
Reprint Please specify: Code Academy >Web Front end > Ajax tips > http://www.uxuew.cn/ajax/6006.html
GBK encoded website using AJAX to receive data Chinese display garbled problem solved