On the data sending side, use the escape function of javascript to convert all Chinese into English encoding and save it in advance (you can also use functions in other languages such as ASP to convert the output in real time).
On the data receiving side, use the unescape function of javascript to restore all English encodings.
Since Ajax can transmit English normally regardless of any encoding, this method can be easily solved.
-------------------------------------------------- ------------------------------------
There are two aspects to the garbled form when using XMLHTTP Post Form. The reason is that the Post form data is garbled in Chinese; the server Response is garbled due to incorrect XMLHTTP encoding. In other words, this article mainly solves two problems - how to correctly post Chinese content & how to correctly display the obtained Chinese content.
Part I Post Chinese content
Let’s take a look at how the E-post form is submitted:
if Replace strA = "submit1=Submit&text1=scsdfsd"; with:
strA = "submit1=Submit&text1=Chinese";
You will find that the stuff submitted is not correct at all, Request.Form("Text1" in ASP ) cannot get the value at all. I used Request.BinaryRead to write out the Post content in an HTML Form and looked at it, only to find the problem - the Form must also be encoded when submitted, and the encoded Chinese characters are escape characters similar to %??%??. For example, "Chinese" is encoded as: ����. Haha, blame me for being stupid, the CONTENT-TYPE clearly states - application/x-www-form-urlencoded, of course urlencoded is what it looks like. In this case, we also know what to do - do the conversion ourselves, the code is as follows:
(Here I put The previous JavaScript code was changed to VBScript. It’s not because I’m full and have nothing to do. The reason will be explained later)
Part II. Display the obtained Chinese content correctly
OK, if you write the content of the Form on the server side If you go to the database/file, there is no problem with the Chinese you see there. However, if you want to see the Server's Response - the problem is: if the result of the Response is not XML, of course there will be nothing in XMLHTTP.responseXML If so, just use responseText. Add this sentence at the end of the code:
alert(oReq.responseText)
Look at the results of our hard work:P
But but...how? All the Chinese characters have turned into squares? (I can’t type it out. If you are interested, you can try it yourself. You don’t need to post it. You can just get a webpage containing Chinese characters and find it. )
The reason is very simple: when XMLHTTP gets the Response, it is assumed that the Response is UTF8 encoded. If the Response is XML, you can also specify the encoding through encoding, but HTML cannot. (Damn GB2312, knock it down again!) So it treats HTML containing GB2312 encoding as UTF8 format, and there will be ghosts only if there is no error!
Fortunately, there is a remedy: the responseBody attribute of XMLHTTP contains an undecoded Response - "a raw undecoded bytes as received directly from the server" :). The only problem is that the responseBody returns An unsigned bytes array, how do we access it and convert it into a BSTR?
This is why I changed the code above to VBScript - VBScript Can do it, but JavaScript Cannot!
The code is below:
----------------------------------------以上为转载-----------------------------
http://www.dhtmlx.com
Start Building Professional Ajax
Web Applications Today
最近用了 DHTMLX的tree,中文的xml居然用不了,报错,整个系统是GB2312的,而DHTMLX只支持UTF8,英文是正常的,中文就出错。
用法也很简单,是DHTMLX 演示里用的代码。
var obj=document.getElementById(oid);
obj.style.display="block";
obj.focus();
if(obj.innerHTML!="")return;
tree=new dhtmlXTreeObject(oid,"100%","100%",0);
tree.setImagePath("js/imgs/csh_vista/");
tree.setOnClickHandler(click1);
tree.setXMLAutoLoading("tree.asp");
//load first level of tree
tree.loadXML("tree.asp?id=0");
tree.asp编码为gb2312,和整个系统一致,用response.write返回一个xml
整个系统编码改不了,只有在DHTMLX上想办法了。
查了些资料,最终解决了。
再深入一层发现是,Microsoft.XMLHTTP的原因,就查到了上面的文件。
如是就简单了,修改如下:
1. 增加一个VBScript的中文转换的函数
2.dhtmlxcommon.js的 getXMLTopNode处
var xmlString = this.xmlDoc.responseText;
改为
var xmlString = bin2str(this.xmlDoc.responseBody);//可以在js里直接调用VBScript脚本
3.同时 在输出xml的asp 文件开头加入
<%response.ContentType="application/xml"%>
当然文件也是保存为gb2312格式的。