In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to do the garbled code in xmlhttp. It is very detailed and has certain reference value. Friends who are interested must finish reading it.
There are two reasons for form garbled when using XMLHTTP Post Form-Chinese garbled when Post form data is used, and garbled code caused by incorrect coding of server Response by XMLHTTP. In other words, this paper mainly solves two problems-how to correctly Post Chinese content and how to display the obtained Chinese content correctly.
Part I Post Chinese content
Let's take a look at how the E-text form is submitted:
The copy code is as follows:
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)
If strA = "submit1=Submit&text1=scsdfsd"; replace it with:
StrA = "submit1=Submit&text1= Chinese"
You will find that what is submitted is not right at all. Request.Form ("Text1") in ASP has no value at all. I used Request.BinaryRead to write out the Post content in a HTML Form and found the problem-- Form should also be encoded when it is submitted, and the encoded Chinese is similar to%. Escape characters, such as "Chinese", are encoded as:% D6%D0%CE%C4. Hehe, I also blame my stupidity. It is clearly written in CONTENT-TYPE-application/x-www-form-urlencoded,urlencoded, of course, is like this. In that case, we also know what to do-- do the conversion ourselves, see the code below:
The copy code is 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
(here I changed the previous JavaScript code to VBScript. I'm not so full that I have nothing to do. See later.)
Part II. Correctly display the obtained Chinese content
OK, if you write the contents of Form to the database / file on the server side, the Chinese you see there is no problem, but if you want to see the Response-- problem of Server: if the result of Response is not XML,XMLHTTP.responseXML, of course there will be nothing, then use responseText and add a sentence at the end of the code:
Alert (oReq.responseText)
Look at the result of our hard work.
But, but... How come all the Chinese words have become squares? (I can't type it out. I'm interested to try 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 also specify the encoding through encoding, but HTML is not. Damn GB2312, knock it down again! ) so it treats HTML with GB2312 code as UTF8 format, and it's a ghost if you don't make mistakes!
Fortunately, there is a remedy: the responseBody property of XMLHTTP contains an undecoded Resonse-- "a raw undecoded bytes as received directly from the server":). The only problem is that responseBody returns an unsigned bytes array, how do we access it and convert it to BSTR?
That's why I changed the code to VBScript above-- VBScript Can do it,but JavaScript Cannot!
The code is as follows:
The copy code is 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
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)
The above is all the contents of the article "what to do with garbled codes in xmlhttp". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.