ajax XMLHTTP Post Form時的表單亂碼綜合解決

來源:互聯網
上載者:User

Part I Post中文內容
先看看E文的表單是怎麼提交的: 複製代碼 代碼如下:<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>

  如果把strA = "submit1=Submit&text1=scsdfsd";換成:strA = "submit1=Submit&text1=中文";
  你會發現提交上去的東東根本不對,ASP中Request.Form("Text1")根本取不到值。俺用Request.BinaryRead把一個HTML Form中的Post內容寫出來看了看,才發現問題——Form提交時也要編碼的,編碼後的中文是類似於%??%??的逸出字元,比如“中文”就被編碼為:%D6%D0%CE%C4。呵呵,也怪俺笨,人家CONTENT-TYPE裡明明寫的清清楚楚——application/x-www-form-urlencoded,urlencoded嘛當然就是這個樣子了。既然這樣,那我們也知道該怎麼辦了——自己做轉換,代碼見下: 複製代碼 代碼如下: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=中文")
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>

(在這裡俺把前面的JavaScript的代碼改成了VBScript,不是吃飽了撐的沒事幹,原因見後)
Part II.正確顯示得到的中文內容
OK,如果你在Server端把Form的內容寫到資料庫/檔案的話,你在那裡看到的中文毫無問題,但是,假如你想看看Server的Response——問題來了:如果Response的結果不是XML,XMLHTTP.responseXML裡當然是不會有東東的,那就用responseText好了,在代碼的最後加一句:alert(oReq.responseText) 。看看俺們辛勤勞動的結果。
  但是但是.....怎麼所有的中文全變成了方格? (我打不出來,有興趣自己去試,也不用Post,Get一個含有中文的網頁就可以發現了。)

  原因很簡單:XMLHTTP得到Response時假定Response是UTF8編碼的,如果Response是XML,那還可以通過encoding來指定編碼,但HTML就不行了。(見鬼的GB2312,再次打倒!)所以它把含GB2312編碼的HTML當成UTF8格式,不出錯才有鬼!

  不過好在還有補救的辦法:XMLHTTP的responseBody 屬性裡包含的可是未解碼的Resonse——"a raw undecoded bytes as received directly from the server" :),唯一的問題是,responseBody返回的是一個unsigned bytes數組,我們怎麼去訪問它,怎麼把它轉換成BSTR?
  這就是為什麼我在上面把代碼改成VBScript的原因——VBScript Can do it,but JavaScript Cannot!
  代碼見下: 複製代碼 代碼如下:<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=中文")
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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.