How to solve XMLHTTP garbled characters in Ajax (utf8, gb2312 encoding and decoding)

Source: Internet
Author: User

51ajax. comajax Forum

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:

Code:[Copy to clipboard] <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 also be encoded during submission. 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,CodeSee:

Code:[Copy to clipboard] <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.

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, 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

For the code, see:

Code:[Copy to clipboard] <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)

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.