Ajax XMLHTTP Post form with the form of garbled integrated solution _ Application Skills

Source: Internet
Author: User
Tags abs chr html form
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: 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:

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 added: alert (oreq.responsetext). Look at the results of our hard work.
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)
</SSRIPT>
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.