As we all know, XMLHTTP in the communication is the UTF code, and a lot of information in the domestic Web page is the use of GBK encoding, so when directly through Ajax to connect the Web page, and will get the information directly displayed will appear garbled phenomenon, sometimes can not change the server side of the page code ( For example, to get other Web site weather information, at this time can only be in the client through JS coding work, the following JS is used to convert server-side GBK encoded strings to UTF encoded strings:
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("%")); }
This code is also found from the Internet, the specific who wrote is not clear, so this does not mark the author, such as the author see also please forgive me.
With this code, you can encode the returned page content directly:
var Response=gb2utf8 (response.responsebody);
After such a transfer code to obtain the content of the page will not have garbled phenomenon;
Of course, if the server-side page uses a utf-8 encoding, there is no need for coding at all.