The code is gb2312. The website displays the data received by Ajax in Chinese.

Source: Internet
Author: User

CopyCode The 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>

below is the full text of this article :
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 Program of the server does not set the Content-Type Response Header, or if no encoding type is set for the header, 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 Code the 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 code The 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.
the solution 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 the value of responsetext or responsexml in UTF-8 format, if the data sent by the server segment is not in UTF-8 format, the value of the received 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 ');
This write unless it is a small project
the big project is based on the Framework
for the header defined in the config file front-end solution
in JS There Is A parsing utf-8xml
recommended
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.