Complete Solution to Chinese garbled characters of Ajax Data Transmission

Source: Internet
Author: User
Tags encode string

[Solution 1]

I used to post a post using XMLHTTP post form. The value of Post e in the Code there is no problem, but it is found that there will be garbled characters when post contains Chinese forms, of course the reason is the conversion problem between UTF-8 and gb2312! Tnnd, down gb2312! How nice is utf8 for everyone.

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:

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

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

Hey, isn't it easy? Use this to try again? Everything is OK!

(By The Way, byte (), the performance of this stuff in VBScript can only be described by the demon-returns 8209 if vartype is called for it -- vbarray + vbbyte, using lbound and ubound can get the upper and lower bounds of the array, but it cannot be accessed in the form of name (I). I thought this type could not be processed in the script, in the bytes2bstr function, we can see that I treat it as a string-lenb/midb or something. I found this was an accident-I started to pass XMLHTTP to this function. responsetext, think of a byte and a byte to see what is in it. Later, I changed responsetext to responsebody, and the result won the prize, haha)

Last nonsense:
1. The above code is passed in the MSXML parser 3 release + VBScript 5.5 environment. The brother has a script of earlier versions. Can you help me to see if it can be achieved.
2. I always thought that JavaScript vs VBScript should be slightly better than JavaScript, so sometimes it may not be a good idea to abandon VBScript and use JavaScript on ASP Server/client.

[Solution 2]

Use escape and Unescape to solve Chinese garbled characters in Ajax applications

Search for Chinese garbled characters in Ajax applications on the Internet and find a method for encoding conversion when storing data.
The principle is to store and extract data from the database, and then encode and convert the data.
I encountered such a problem before, but I didn't find a solution at the time.
Now it seems that this method should be feasible. I haven't tried it yet. Remember it first!

Escape Method

Description
Encode string objects so that they can be read on all computers,
Syntax
Escape (charstring)
The charstring parameter is a string object to be encoded.

Description
Escape returns a New String object (in unicode format) containing charstring content. All spaces, punctuation marks, accents, and other non-ASCII characters are replaced by % XX encoding, XX indicates the hexadecimal number of the character. For example, "% 20" is returned by a space ".
The characters with a value greater than 255 are stored in % uxxxx format.

Unescape Method

Description
Decodes string objects encoded using the escape method.
Syntax
Unescape (charstring)
The charstring parameter is a string object to be decoded.

Description
The Unescape method returns a New String object that saves charstring content. All characters encoded in % XX hexadecimal format are replaced by characters of medium price in the ASCII character set.
Characters encoded in % uxxxx format (UNICODE character) are replaced by Unicode characters encoded in hexadecimal format XXXX.

[Solution 3]

When you use objxml. responsetext to obtain the Chinese output of response. write on a page, it will be garbled. I checked the information yesterday and found a solution, so I posted it.

Response. charset = "gb2312"

Response. Write ("Chinese is displayed normally! ")

Haha, But I didn't display Chinese characters. Ajax itself is to reduce the server's bandwidth usage, So I directly response. Write ("1 ")

Then judge on the client

If (objxml. responsetext = 1)
OBJ. innerhtml = "the data you entered is invalid ";

In other languages, I Don't estimate that I can set the output language encoding to gb2312.

When Ajax is used to get back to a page, most of the Chinese characters in responsetext will be garbled, this is because XMLHTTP in processing the returned responsetext, is the resposebody according to the UTF-8 encoding into the decoding test form, if the server sends a data stream that is indeed a UTF-8, the Chinese characters will be correctly displayed, and when the GBK encoding stream is sent out, it will be messy. The solution is to add a header in the sent stream to specify the encoding stream to be sent, so that XMLHTTP will not be messed up.

PHP: Header ('content-type: text/html; charset = gb2312 ′);
ASP: Response. charset ("gb2312 ″)
JSP: Response. setheader ("charset", "gb2312 ″);

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 888152

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.