The code is GB2312, so that the data received by Ajax can be displayed in Chinese

Time:2021-12-22
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”; // It is used to record whether Firefox is used for the following processing and receiving Chinese data analysis.
}
}
function querytelcode(citys){
createXML();
showState=document.getElementById(“showtelcode”);
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 2){
showState.innerHTML = ‘<img src=”/img/process.gif” />’;
}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”);// It is set to identify data with GB2312 code, which is only supported by FF. IE can’t
}
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 following is the full text of this article:
XMLHttpRequest passes data in UTF-8 by default. When the returned data of the server is UTF-8 coding, it works well (developing web applications, when the server, client and database use UTF-8 coding uniformly, it can effectively avoid the problem of garbled code). If the server has set the correct content type response header and encoding information, XMLHttpRequest can also work correctly.
However, when using XMLHttpRequest to read Chinese Web page content, if the server program does not set the content type response header, or the header does not set the encoding type, we may encounter garbled code when accessing the responseText property. As shown in the following code, use XMLHttpRequest to obtain the constellation home page of 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 such as Yahoo China, the support for web standards is not complete. The pop-up HTML source code is full of HTML tags that do not meet the web standards, and of course, there are foreseen random codes.
It is also a pity that the solutions of Firefox and ie are also different
Firefox approach:
The XMLHttpRequest object of Firefox supports the overridemimetype method, which can specify the encoding type of the returned data. This method can be used to solve Chinese garbled code. The previous 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”);// Set to identify data with GB2312 code
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 by a very poor method. At this time, a hybrid function needs to be introduced:

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)); // Note that the ResponseBody should be used here
};
xmlhttp.send(null);

The gb2utf8 function directly parses the binary data returned by XMLHttpRequest, in which the execscript method is used to execute the VBScript function. So it’s a hybrid function. Thanks for the algorithm provided by blueidea forum.
Although there is a solution, it is ugly and does not comply with web standards. Therefore, it should be avoided in programming. If developing web applications, UTF-8 coding should be used as much as possible, or the correct coding information should be set at the server. As for the above examples, they are suspected of stealing the content of other websites, and they are not advocated.
Attachment 1: Discussion:
Reason: Ajax decodes the responseText or responsexml value in UTF-8 format. If the data sent by the server segment is not in UTF-8 format, the received responseText or responsexml value may be garbled.
Solution: specify the format of sending data on the server:
In the JSP file:
response. setContentType(“text/text;charset=UTF-8”);// The returned is a TXT text file
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″);
Discussion:
PHP:header(‘Content-Type:text/html;charset=GB2312’);
Unless it’s a small project
Large projects follow the framework
For the front-end solution defined in the config file for header
There is a method for parsing UTF-8 XML in JS, which is very strong
I suggest you use that
Nonphysical life traindiy

Recommended Today

Review: data structure and algorithm – 07 queue

xwzz.jpg Review: data structures and algorithms – Opening Review: data structure and algorithm – complexity analysis (I) Review: data structure and algorithm – complexity analysis (II) Review: data structures and algorithms – arrays Review: data structure and algorithm – linked list (I) Review: data structure and algorithm – linked list (2) Review: data structures and […]