The code is GB2312. The website displays the data received by AJAX in Chinese.

Source: Internet
Author: User

Copy codeThe 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 analysis of received Chinese data.
}
}
Function querytelcode (citys ){
CreateXML ();
ShowState = document. getElementById ("showtelcode ");
XmlHttp. onreadystatechange = function (){
If (xmlHttp. readyState = 2 ){
ShowState. innerHTML = ' ';
} Else if (xmlHttp. readyState = 4 & xmlHttp. status = 200 ){
If (BrowerType = "ff "){
GetLastCode = xmlHttp. responseText; // FireFox
} Else {
GetLastCode = gb2utf8 (xmlHttp. responseBody );
}
ShowState. innerHTML = getLastCode;
}
}
Var url = 'myorderajax. asp? Oid = <% = Request. QueryString ("orderid") %> & cityname = '+ citys;
XmlHttp. open ("GET", url, true );
If (BrowerType = "ff "){
XmlHttp. overrideMimeType ("text/html; charset = gb2312"); // sets to identify data in gb2312 encoding, only FF supports. IE does not work
}
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>

The full text of this article is as follows:
XMLHttpRequest transfers data with a UTF-8 by default. When the returned data from the server is UTF-8 encoding, it works very well (developing web applications, when the server and client and database unified use of UTF-8 coding can effectively avoid garbled problem ). If the server sets the correct Content-Type Response Header and encoding information, XmlHttpRequest can also work correctly.
However, when XMLHttpRequest is used to read Chinese webpage Content, if the Content-Type Response Header is not set in the program on the server side, or the encoding Type is not set in the Header, then we may encounter garbled characters when accessing the responseText attribute. The following code uses XMLHttpRequest to obtain the homepage of the constellation station on Yahoo China Website:
Copy codeThe Code is as follows:
Xmlhttp = getXMLHttpRequest ();
Var url = "http://cn.astrology.yahoo.com /";;
Xmlhttp. open ("GET", url, true );
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readyState = 4)
If (xmlhttp. status = 200)
Alert (xmlhttp. responseText );
};
Xmlhttp. send (null );

Even for professional websites like yahoo China, the support for web standards is not thorough. the pop-up html source code is filled with html tags that do not comply with web standards, and of course there are foreseeable garbled characters.
Unfortunately, FireFox and IE are both solutions.
FireFox practices:
The XMLHttpRequest object of FireFox supports the overrideMimeType method. You can specify the encoding type of the returned data. This method can solve Chinese garbled characters. The preceding code is modified as follows:
Copy codeThe Code is as follows:
Xmlhttp = getXMLHttpRequest ();
Var url = "http://cn.astrology.yahoo.com /";;
Xmlhttp. open ("GET", url, true );
Xmlhttp. overrideMimeType ("text/html; charset = gb2312"); // sets to identify data in gb2312 encoding.
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 bad way. In this case, you need to introduce a hybrid function:
Copy codeThe Code is as follows:
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://cn.astrology.yahoo.com /";;
Xmlhttp. open ("GET", url, true );
Xmlhttp. onreadystatechange = function (){
If (xmlhttp. readyState = 4)
If (xmlhttp. status = 200)
Alert (gb2utf8 (xmlhttp. responseBody); // be sure to use responseBody here.
};
Xmlhttp. send (null );

The gb2utf8 function directly parses the binary data returned by XMLHttpRequest. execScript is used to execute the VBScript function. So it is a hybrid function. Thanks to the algorithm provided by the blueidea forum.
Although there is a solution, the form is ugly and does not comply with web standards. So it should be avoided in programming, if it is the development of web applications, should try to use UTF-8 encoding, or set the correct encoding information on the server. As for the above examples, it is not recommended to steal content from other websites.
Appendix: discussion:
Cause: AJAX decodes responseText or responseXML values in UTF-8 format if the data sent by the server segment is not in UTF-8 format, the value of responseText or responseXML may be garbled.
Solution: Specify the data sending format 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 ");
Discussion:
PHP: header ('content-Type: text/html; charset = GB2312 ');
Unless it is a small project.
All major projects follow the framework
Front-End Solution for HEADER defined in config file
In JS There Is A parsing utf-8xml method written very strong
We recommend that you use
Non-physical life traindiy

Related Article

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.