Encoding for GB2312 Web sites lets Ajax receive data showing support for Chinese _ajax related

Source: Internet
Author: User
Copy Code code 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 the following processing 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 = 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"),//set to GB2312 encoding to identify the data, only FF support. IE No.
}
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&LT;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 following is the full text of this article:
XMLHttpRequest the default is to pass data with UTF-8. When the server-side return data is UTF-8 encoded, it works well (developing Web applications, when the server and client and database unified use of UTF-8 encoding can effectively avoid garbled problems). If the server is set up with the correct Content-type Response header and encoded information, then XMLHttpRequest can also work correctly.
However, when using XMLHttpRequest to read Chinese Web page content, if the server-side program does not set the Content-type Response header, or the header does not set the encoding type, Then we can access the ResponseText properties of the time may encounter garbled. For example, the following code uses XMLHttpRequest to get the home page of Yahoo China website:
Copy Code code 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 if Yahoo China such a professional website, the support of the Web standards is still very incomplete, pop-up HTML source code is flooded with Web-standard HTML tags, of course, there are predictable garbled.
It is also regrettable that FireFox and IE solutions are also diametrically opposed
FireFox Practices:
FireFox XMLHttpRequest Object supports the Overridemimetype method, you can specify the encoding type of return data, using this method can solve Chinese garbled, the previous code changes as follows:
Copy Code code as follows:

XMLHTTP = Getxmlhttprequest ();
var url = "http://cn.astrology.yahoo.com/";;
Xmlhttp.open ("Get", url, True);
Xmlhttp.overridemimetype ("text/html;charset=gb2312");/set to identify data by 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, and a hybrid function needs to be introduced at this point:
Copy Code code 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&LT;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)); Watch out for responsebody here.
};
Xmlhttp.send (NULL);

The Gb2utf8 function directly resolves the binary data returned by the XMLHttpRequest, which uses the Execscript method to perform the functions of VBScript. So that's a hybrid function. Thanks to the algorithm provided by the Blueidea forum.
Although there are solutions, the form is ugly and does not conform to web standards. So you should try to avoid in programming, if you are developing Web applications, you should try to use UTF-8 encoding, or set the correct encoding information on the server. As for the above example, there is suspicion of stealing other website content, and it is not advocating.
Attached: discussion:
Reason: Ajax in the reception of ResponseText or Responsexml value is in accordance with the format of UTF-8 decoding, if the server segment sent data is not UTF-8 format, Then the value of receiving responsetext or responsexml may be garbled.
Workaround: Specify the format for sending data on the server:
In the JSP file:
Response.setcontenttype ("text/text;charset=utf-8")/txt text file returned
Or
Response.setcontenttype ("Text/xml;charset=utf-8")//returned XML file
Php:header (' content-type:text/html;charset=gb2312 ');
ASP:Response.Charset ("GB2312")
JSP:response.setHeader ("Charset", "GB2312");
Discuss:
Php:header (' content-type:text/html;charset=gb2312 ');
Write this unless it's a small project.
Big projects are all in the frame.
Most front-end solution for header defined in config file
In JS there is a parse Utf-8xml method written very strong
Suggested with that
Non-physical life Traindiy

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.