XMLHTTP garbled solution (utf8,gb2312 code decoding) _javascript techniques

Source: Internet
Author: User
Tags abs html form
On the data sender side, use JavaScript's escape function to convert all Chinese into English encoding and saving (or to convert the output in real time using other languages such as ASP).
On the receiving side of the data, use JavaScript's unescape function to restore all English encodings.
This method can be easily solved because Ajax can transmit the English language normally, regardless of any encoding.

--------------------------------------------------------------------------------
When using XMLHTTP Post form, there are two reasons for the--post of the form when the form data is garbled, and the server response is XMLHTTP incorrectly encoded. In other words, this article mainly solves two problems-how to post Chinese content correctly & how to correctly display the Chinese content.
Part I post Chinese content
First look at how the form E is submitted:
Copy Code code as follows:

<script language= "JavaScript" >
Stra = "SUBMIT1=SUBMIT&TEXT1=SCSDFSD";
var oreq = new ActiveXObject ("MSXML2. XMLHTTP ");
Oreq.open ("POST", "http://ServerName/VDir/TstResult.asp", false);
Oreq.setrequestheader ("Content-length", stra.length);
Oreq.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Oreq.send (Stra);
</ScRIPT>

If the Stra = "SUBMIT1=SUBMIT&AMP;TEXT1=SCSDFSD", replace it with:
Stra = "submit1=submit&text1= Chinese";
You will find that the things submitted to the wrong, ASP Request.Form ("Text1") can not get the value. I use Request.BinaryRead to write a post in an HTML form to look at, only to find the problem--form submitted to the code, after the encoding of Chinese is similar to%?? %?? Escape characters, such as "Chinese", are encoded as:%D6%D0%CE%C4. Oh, also blame me stupid, someone else content-type clearly write clearly--application/x-www-form-urlencoded,urlencoded well, of course it is this appearance. In this way, we also know what to do-to do the conversion, the code see below:
Copy Code code as follows:

<script language= "VBScript" >
Function urlencoding (Vstrin)
Strreturn = ""
For i = 1 to Len (Vstrin)
THISCHR = Mid (vstrin,i,1)
If Abs (ASC (THISCHR)) < &hff Then
Strreturn = Strreturn & THISCHR
Else
Innercode = ASC (THISCHR)
If Innercode < 0 Then
Innercode = Innercode + &h10000
End If
Hight8 = (Innercode and &hff00) \ &hff
Low8 = Innercode and &hff
Strreturn = strreturn & "%" & Hex (HIGHT8) & "%" & Hex (LOW8)
End If
Next
urlencoding = Strreturn
End Function
Stra = urlencoding ("submit1=submit&text1= Chinese")
Oreq = CreateObject ("MSXML2"). XMLHTTP ")
Oreq.open "POST", "http://ServerName/VDir/TstResult.asp", False
Oreq.setrequestheader "Content-length", Len (Stra)
Oreq.setrequestheader "Content-type", "application/x-www-form-urlencoded"
Oreq.send Stra
</SCRIPT>

(Here I changed the previous JavaScript code to VBScript, not full of nothing to do, after the reason see)
Part ii. correct display of Chinese content
OK, if you write the form content to the database/file on the server side, you will see no problem in Chinese there, but if you want to see the response--problem with the server: If the response result is not XML, Xmlhttp.responsexml of course there will be no dongdong, then use ResponseText good, at the end of the code add a sentence:
Alert (Oreq.responsetext)
Look at the results of our hard work:P
But ... but. How all the Chinese have become squares? I can't get it out, I'm interested in trying it myself, and I don't have to post,get a Web page containing Chinese to find it. )
The reason is simple: when XMLHTTP gets response, it assumes that response is UTF8 encoded, and if response is XML, you can specify the encoding by encoding, but HTML is not. (Damn GB2312, knock it down again!) So it has GB2312 encoded HTML as a UTF8 format, not a mistake to have a ghost!
But fortunately there is a remedy: XMLHTTP's Responsebody attribute contains the resonse--"a raw undecoded bytes as received directly from the server": the only The problem is, Responsebody returns a unsigned bytes array, how do we access it, how do we convert it to BSTR?
That's why I changed the code to VBScript on top--vbscript Can do it,but JavaScript cannot!
Code See below:
Copy Code code as follows:

<script language= "VBScript" >
Function urlencoding (Vstrin)
Strreturn = ""
For i = 1 to Len (Vstrin)
THISCHR = Mid (vstrin,i,1)
If Abs (ASC (THISCHR)) < &hff Then
Strreturn = Strreturn & THISCHR
Else
Innercode = ASC (THISCHR)
If Innercode < 0 Then
Innercode = Innercode + &h10000
End If
Hight8 = (Innercode and &hff00) \ &hff
Low8 = Innercode and &hff
Strreturn = strreturn & "%" & Hex (HIGHT8) & "%" & Hex (LOW8)
End If
Next
urlencoding = Strreturn
End Function
Function Bytes2bstr (vIn)
Strreturn = ""
For i = 1 to LenB (vIn)
Thischarcode = AscB (MidB (vin,i,1))
If Thischarcode < &h80 Then
Strreturn = Strreturn & Chr (Thischarcode)
Else
Nextcharcode = AscB (MidB (vin,i+1,1))
Strreturn = Strreturn & Chr (CLng (thischarcode) * &h100 + CInt (nextcharcode))
i = i + 1
End If
Next
Bytes2bstr = Strreturn
End Function
Stra = urlencoding ("submit1=submit&text1= Chinese")
Oreq = CreateObject ("MSXML2"). XMLHTTP ")
Oreq.open "POST", "http://ServerName/VDir/TstResult.asp", False
Oreq.setrequestheader "Content-length", Len (Stra)
Oreq.setrequestheader "Content-type", "application/x-www-form-urlencoded"
Oreq.send Stra
Alert Bytes2bstr (Oreq.responsebody)
</SCRIPT>

----------------------------------------above for reprint-----------------------------
Http://www.dhtmlx.com

Start Building Professional Ajax
Web Applications Today
Recently used DHTMLX tree, Chinese XML can not use, error, the whole system is GB2312, and DHTMLX only support UTF8, English is normal, Chinese error.

The usage is also very simple, is the code used in the DHTMLX demo.
Copy Code code as follows:

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 the
Tree.loadxml ("Tree.asp?id=0");

Tree.asp encoded as gb2312, consistent with the entire system, with Response.Write returns an XML
The entire system code can not change, only in the DHTMLX to find a way.
I checked some information and finally solved it.


A further layer of discovery is that the microsoft.xmlhttp of the reason, the above file was found.
If it is simple, modify the following:
1. Add a function of the Chinese conversion of VBScript
Copy Code code as follows:

<script language= "VBScript" >
' Handling Chinese
function Bin2str (BIN)
Dim tmp,ustr,i
Tmp= ""
For I=1 to LenB (bin)-1
USTR=ASCB (MidB (bin,i,1))
If ustr>127 Then
I=i+1
TMP=TMP&CHR (USTR*256+ASCB (MidB (bin,i,1))
Else
TMP=TMP&CHR (USTR)
End If
Next
Bin2str=tmp
End Function
</script>

2.dhtmlxcommon.js's Getxmltopnode place.
var xmlstring = This.xmlDoc.responseText;
To
var xmlstring = bin2str (this.xmlDoc.responseBody);//Can call VBScript script directly in JS

3. Join at the beginning of ASP file of output XML
<%response. Contenttype= "Application/xml"%>
<?xml version= ' 1.0 ' encoding= ' GB2312 '?>
Of course, the file is also saved in gb2312 format.

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.