I used to post a post with XMLHTTP post Form, where the code I post e of the value of no problem, but later found that Post contains Chinese form will appear garbled, the reason is of course, UTF-8 and GB2312 between the conversion problem! Tnnd, down gb2312!. It's good for everyone to use UTF8.
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.
If the Stra = "SUBMIT1=SUBMIT&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:
<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
(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:
<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
Hey, is not very simple ah, with this try again? Everything ok!
(By the way Byte (), this dongdong performance in the VBScript can only use the demon to describe-to it call VarType return 8209--vbarray + vbbyte, with LBound, UBound can get the upper bound of the array, but is not the name (i I thought I couldn't handle this type in the script, and in the BYTES2BSTR function I saw it as a string to deal with--LENB/MIDB or something, and it was an accident-- I began to pass to this function is Xmlhttp.responsetext, want a byte a byte to see inside exactly what, later on a temporary responsetext changed to Responsebody, the results of the lottery, haha)
The last of the nonsense:
1. The above code is passed in MSXML Parser 3 release+vbscript 5.5 environment. The brother has an earlier version of the script that can help me try to see if I can make it.
2, always thought JavaScript vs VBScript should be JavaScript slightly better, so sometimes want to completely discard vbscript,asp server/client all use JavaScript, it seems not a good idea.
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.