- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > ASP >
- Asp中使用JQuery的AJAX提交中文亂碼解決方法
客戶(hù)端頁(yè):client.html
代碼如下:
<script>
//jquery的post
$.post
(
'server.asp',
{
Act:'DoSubmit',
UserName:escape('腳本之家'),//進(jìn)行編碼
WebSite:'www.jb51.net'
},
function(data)
{
alert(unescape(data));//對返回數據進(jìn)行解碼
}
);
</script>
服務(wù)器端頁(yè):server.asp
代碼如下:
< %
Response.Charset="gb2312"
Dim UserName,WebSite
If Request.Form("Act")="DoSubmit" Then
UserName=Request.Form("UserName")
WebSite =Request.Form("WebSite")
'在服務(wù)器端解碼
UserName=VbsUnEscape(UserName)//解碼
'處理數據
'---省略數據處理部分
'數據處理后輸出,先用VbsEscape()編碼
Response.Write VbsEscape(UserName)
End If
%>
< %
'與javascript中的escape()等效
Function VbsEscape(str)
dim i,s,c,a
s=""
For i=1 to Len(str)
c=Mid(str,i,1)
a=ASCW(c)
If (a>=48 and a< =57) or (a>=65 and a< =90) or (a>=97 and a< =122) Then
s = s & c
ElseIf InStr("@*_+-./",c)>0 Then
s = s & c
ElseIf a>0 and a<16 Then
s = s & "%0" & Hex(a)
ElseIf a>=16 and a<256 Then
s = s & "%" & Hex(a)
Else
s = s & "%u" & Hex(a)
End If
Next
VbsEscape=s
End Function
'與javascript中的unescape()等效
Function VbsUnEscape(str)
Dim x
x=InStr(str,"%")
Do While x>0
VbsUnEscape=VbsUnEscape&Mid(str,1,x-1)
If LCase(Mid(str,x+1,1))="u" Then
VbsUnEscape=VbsUnEscape&ChrW(CLng("&H"&Mid(str,x+2,4)))
str=Mid(str,x+6)
Else
VbsUnEscape=VbsUnEscape&Chr(CLng("&H"&Mid(str,x+1,2)))
str=Mid(str,x+3)
End If
x=InStr(str,"%")
Loop
VbsUnEscape=VbsUnEscape&str
End Function
%>
在javascript 中escape() 函數可對字符串進(jìn)行編碼,這樣就可以在所有的計算機上讀取該字符串。
可以使用 unescape() 對 escape() 編碼的字符串進(jìn)行解碼。
其實(shí)Asp中這兩個(gè)函數也是起作用的,居然很多asp網(wǎng)站上沒(méi)有進(jìn)行介紹。
要不然只能像上面那樣寫(xiě)函數進(jìn)行解碼編碼了。復雜且性能不好。
上面的服務(wù)器端頁(yè):server.asp可以寫(xiě)成:
Asp中的unescape() 與 escape() 函數
代碼如下:
< %
Response.Charset="gb2312"
Dim UserName,WebSite
If Request.Form("Act")="DoSubmit" Then
UserName=Request.Form("UserName")
WebSite =Request.Form("WebSite")
'在服務(wù)器端解碼
UserName=UnEscape(UserName)//解碼
'處理數據
'---省略數據處理部分
'數據處理后輸出,先用VbsEscape()編碼
Response.Write Escape(UserName)
End If
%>
這樣就簡(jiǎn)單多了。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站