asp传递给js,javascript中文乱码的问题,gbk乱码解决办法

[ 2015-08-31 16:51:27 | 作者: admin ]
字号: | |
因为js获取的都是utf-8编码,当asp传递gbk中文给js的时候,就会出现乱码。

javascript中使用 escape()和unescape(),分别是编码和解码字符串。

asp中使用相应的自定义函数来解决中文编码加密escape和解密unescape:
<%
'加密
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

'解密
Function vbsUnEscape(str)
         dim i,s,c
         s=""
         For i=1 to Len(str)
                c=Mid(str,i,1)
                If Mid(str,i,2)="%u" and i<=Len(str)-5 Then
                     If IsNumeric("&H" & Mid(str,i+2,4)) Then
                     s = s & CHRW(CInt("&H" & Mid(str,i+2,4)))
                     i = i+5
                     Else
                     s = s & c
                     End If
                ElseIf c="%" and i<=Len(str)-2 Then
                     If IsNumeric("&H" & Mid(str,i+1,2)) Then
                     s = s & CHRW(CInt("&H" & Mid(str,i+1,2)))
                     i = i+2
                     Else
                     s = s & c
                     End If
                Else
                     s = s & c
                End If
         Next
         vbsUnEscape = s
End Function
%>
[最后修改由 admin, 于 2015-08-31 16:55:27]
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=2291

这篇日志没有评论。

此日志不可发表评论。