GBK encoded website using AJAX to receive data Chinese display garbled problem solved

Source: Internet
Author: User

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:

  1. xmlhttp = getxmlhttprequest ();  
  2. var url = 
  3. xmlhttp.open ( "GET",  url, true);  
  4. xmlhttp.onreadystatechange = function () { 
  5. if  (xmlhttp.readystate == 4)  
  6. if  (xmlhttp.status == 200)  
  7. alert ( Xmlhttp.responsetext);  
  8. }; 
  9. 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:

  1. xmlhttp = getxmlhttprequest ();  
  2. var url =  "http://cn.astrology.yahoo.com/";;  
  3. xmlhttp.open ( "GET",  url, true);  
  4. xmlhttp.overridemimetype (//setting gb2312 encoding to identify data  
  5. xmlhttp.onreadystatechange = function () { 
  6. if  (xmlhttp.readystate == 4)  
  7. if  (xmlhttp.status == 200)  
  8. alert (xmlhttp.responsetext);  
  9. }; 
  10. 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:

  1. function Gb2utf8 (data) {
  2. var glbencode = [];
  3. Gb2utf8_data = data;
  4. ExecScript ("gb2utf8_data = MidB (gb2utf8_data, 1)", "VBScript");
  5. var t=escape (gb2utf8_data). Replace (/%u/g,""). Replace (/(. { 2}) (. { 2})/g,"%$2%$1"). Replace (/% ([A-z].)% (. {2}) /g,"@$1$2");
  6. T=t.split ("@");
  7. var i=0,j=t.length,k;
  8. while (++I<J) {
  9. K=t[i].substring (0,4);
  10. if (!glbencode[k]) {
  11. Gb2utf8_char = eval ("0x" +k);
  12. ExecScript ("Gb2utf8_char = Chr (Gb2utf8_char)", "VBScript");
  13. Glbencode[k]=escape (Gb2utf8_char). substring (1,6);
  14. }
  15. T[i]=glbencode[k]+t[i].substring (4);
  16. }
  17. Gb2utf8_data = Gb2utf8_char = null;
  18. Return unescape (T.join ("%"));
  19. }
  20. XMLHTTP = Getxmlhttprequest ();
  21. var url = "http://www.uxuew.cn/";;
  22. Xmlhttp.open ("GET", url, true);
  23. Xmlhttp.onreadystatechange = function () {
  24. if (xmlhttp.readystate = = 4)
  25. if (Xmlhttp.status = = 200)
  26. Alert (Gb2utf8 (xmlhttp.responsebody)); //Note here to use Responsebody
  27. };
  28. 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:

  1. In the JSP file:
  2. Response.setcontenttype ("Text/text;charset=utf-8"); The txt text file is returned
  3. Or
  4. Response.setcontenttype ("Text/xml;charset=utf-8"); The returned XML file
  5. Php:header (' content-type:text/html;charset=gb2312 ');
  6. ASP:Response.Charset ("GB2312")
  7. JSP:response.setHeader ("Charset","GB2312");

The complete code is as follows:

  1. <script>
  2. var xmlHttp;
  3. var browertype="ie";
  4. function Createxml () {
  5. try{
  6. XmlHttp = new ActiveXObject ("msxml2.xmlhttp");
  7. } catch (e) {
  8. try{
  9. XmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
  10. }catch (E2) {
  11. XmlHttp =false;
  12. }
  13. }
  14. if (!xmlhttp && typeof XMLHttpRequest! = ' undefined ') {
  15. XmlHttp = new XMLHttpRequest ();
  16. Browertype = "FF"; //used to record whether Firefox is used for processing below to receive Chinese data analysis.
  17. }
  18. }
  19. function Querytelcode (citys) {
  20. Createxml ();
  21. Showstate=document.getelementbyid ("Showtelcode");
  22. Xmlhttp.onreadystatechange = function () {
  23. if (xmlhttp.readystate = = 2) {
  24. showstate.innerhtml = ' ';
  25. }Else if (xmlhttp.readystate = = 4 && Xmlhttp.status = =) {
  26. if (browertype=="ff") {
  27. Getlastcode=xmlhttp.responsetext; //firefox
  28. }else{
  29. Getlastcode=gb2utf8 (Xmlhttp.responsebody);
  30. }
  31. showstate.innerhtml = Getlastcode;
  32. }
  33. }
  34. var url=' http://www.uxuew.cn?oid=<%=Request.QueryString ("OrderID")%>&cityname= ' +citys;
  35. Xmlhttp.open ("GET", url,true);
  36. if (browertype=="ff") {
  37. Xmlhttp.overridemimetype ("text/html;charset=gb2312"); Set to identify data with gb2312 encoding, only FF support. ie not
  38. }
  39. Xmlhttp.send (null);
  40. }
  41. function Gb2utf8 (data) {
  42. var glbencode = [];
  43. Gb2utf8_data = data;
  44. ExecScript ("gb2utf8_data = MidB (gb2utf8_data, 1)", "VBScript");
  45. var t=escape (gb2utf8_data). Replace (/%u/g,""). Replace (/(. { 2}) (. { 2})/g,"%$2%$1"). Replace (/% ([A-z].)% (. {2}) /g,"@$1$2");
  46. T=t.split ("@");
  47. var i=0,j=t.length,k;
  48. while (++I<J) {
  49. K=t[i].substring (0,4);
  50. if (!glbencode[k]) {
  51. Gb2utf8_char = eval ("0x" +k);
  52. ExecScript ("Gb2utf8_char = Chr (Gb2utf8_char)", "VBScript");
  53. Glbencode[k]=escape (Gb2utf8_char). substring (1,6);
  54. }
  55. T[i]=glbencode[k]+t[i].substring (4);
  56. }
  57. Gb2utf8_data = Gb2utf8_char = null;
  58. Return unescape (T.join ("%"));
  59. }
  60. </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

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.