XMLHTTP garbled solution (UTF8, GB2312 encoding and decoding)

Source: Internet
Author: User

On the data sender side, use the javascript escape Function to convert all Chinese characters into English encoding and save them (you can also use functions in other languages such as ASP to convert the output in real time ).
On the data recipient side, use the javascript unescape function to restore all English codes.
Since Ajax can be transmitted in English normally regardless of the encoding, this method can be easily used.

--------------------------------------------------------------------------------
There are two possible causes for Form garbled characters when using XMLHTTP Post Form: Chinese garbled characters in Post Form data; and Server Response garbled characters caused by incorrect XMLHTTP encoding. In other words, this article mainly solves two problems: how to correctly Post Chinese content and how to correctly display the obtained Chinese content.
Part I Post Chinese content
First, let's take a look at how E-text forms are submitted:
Copy codeThe Code is 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 you replace strA = "submit1 = Submit & text1 = scsdfsd":
StrA = "submit1 = Submit & text1 = Chinese ";
You will find that the submitted stuff is not correct at all, and the Request. Form ("Text1") in ASP cannot get the value at all. I used Request. BinaryRead to write the Post content in an HTML Form and read it out. I found the problem-the Form must be encoded when submitted. The Encoded chinese is similar to % ?? % ?? For example, "Chinese" is encoded as: % D6 % D0 % CE % C4. Well, it's also silly. The CONTENT-TYPE clearly states that application/x-www-form-urlencoded and urlencoded are exactly the same. In this case, we also know what to do-convert ourselves. For the code, see:
Copy codeThe Code is 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, which is not enough to do. For the reason, see later)
Part II. Correctly display the obtained Chinese content
OK. If you write the Form content to the database/file on the Server side, there is no problem with the Chinese text you see there. However, if you want to see the Server's Response -- the problem is: if the Response result is not XML, XMLHTTP. of course there will be no stuff in responseXML, so use responseText. add a sentence at the end of the Code:
Alert (oReq. responseText)
Let's look at the results of our hard work: P
But how can all Chinese characters be changed to squares? (I can't try it out. If I want to try it myself, I don't need to Post it. Get a webpage containing Chinese characters and I will find it .)
The reason is simple: when XMLHTTP gets Response, it is assumed that Response is UTF8 encoded. If Response is XML, you can also specify encoding through encoding, but HTML will not work. (Go to hell !) Therefore, it treats HTML with GB2312 Encoding As UTF8 format, so there is no error!
But there is still a remedy: the undecoded Resonse included in the responseBody attribute of XMLHTTP -- "a raw undecoded bytes as decoded ed directly from the server" :), the only problem is that, responseBody returns an unsigned bytes array. How can we access it and convert it to BSTR?
This is why I changed the code above to VBScript-VBScript Can do it, but JavaScript Cannot!
For the code, see:
Copy codeThe Code is 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>

---------------------------------------- The above is for Reprinted -----------------------------
Http://www.dhtmlx.com

Start Building Professional Ajax
Web Applications Today
Recently, DHTMLX tree is used. xml in Chinese cannot be used, and an error is reported. The entire system is GB2312, while DHTMLX only supports UTF8. English is normal, and errors occur in Chinese.

The usage is also very simple. It is the code used in the DHTMLX demonstration.
Copy codeThe Code is 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 first level of tree
Tree. loadXML ("tree. asp? Id = 0 ");

Tree. asp is encoded as gb2312, which is consistent with the entire system. response. write returns an xml
The entire system code cannot be changed. You only need to find a solution on DHTMLX.
I checked some information and finally solved the problem.


The cause of Microsoft. XMLHTTP is that the above file is found.
The modification is as follows:
1. added a VBScript Chinese conversion function.
Copy codeThe Code is as follows:
<Script language = "VBScript">
'Process Chinese Characters
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. getXMLTopNode of dhtmlxcommon. js
Var xmlString = this. xmlDoc. responseText;
Change
Var xmlString = bin2str (this. xmlDoc. responseBody); // you can directly call the VBScript script in js.

3. Add
<% Response. ContentType = "application/xml" %>
<? Xml version = '1. 0' encoding = 'gb2312 '?>
Of course, the file is also saved in gb2312 format.

Related Article

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.