出现中文乱码的原因,总结起来就是因为页面编码不一致的问题。常见的就是gb2312编码页面和utf8编码页面的中文数据传输。
解决思路:对中文数据进行编码或加密成为字母+数字的密文,在接收页面再进行解码或解密,因为字母和数字没有乱码的困扰,
这样就避免了中文乱码的问题。常见的是使用开发平台自带的编码/解码、加密/解密函数,也可以引入外来的函数。如aes对称加密函数等等。
1.Aspx页面中,Url链接有中文乱码
解决办法,对中文部分进行编码,这样就不会有编码问题导致的乱码了。
假设Url的部分参数,Title=我是中文
Server.UrlEncode(Title) 用来编码的
Server.UrlDecode(Request.QueryString["Title"]) 用来解码的
< a href = " /Product/ProductPhotoDes.aspx?FPrice=0&TPrice=100&Title=
<!-- 对获取的Title参数进行解码 -->
m_SecondName = Server.UrlDecode( Request.QueryString[ " Title " ]);
2.javascript中文参数乱码
假设Url的部分参数,Title=我是中文
escape(Title) javascript用来编码的
unescape(Title) javascript用来解码的
context.Server.UrlDecode(context.Request.QueryString["Title"]) Ashx用来解码的
3.javascript的3个编码函数
escape/unescape
除了ASCII 字母、数字和特定的符号外,对传进来的字符串全部进行转义编码;
因此如果想对URL编码,最好不要使用此方法;
escape()适合对url的参数进行编码。
encodeURI/decodeURI
用于编码整个URL,因为URL中的合法字符都不会被编码转换;
进行url跳转时可以整体使用encodeURI。
例如:Location.href=encodeURI("http://www.XX.com/Product/ProductShow.aspx?pid=468&SecondClass=35");
encodeURIComponent/decodeURIComponents
对url的参数编码,特别在参数也是一个url的情况,如果不编码会影响整个url跳转。
例如:document.write('<a href="http://passport.baidu.com/?logout&aid=7&
u='+encodeURIComponent("http://cang.baidu.com/bruce42")+'">退出</a>');
4.Ajax和Asp交互中,服务器端回传给客户端时,javascript获取中文乱码
response.charset = " gb2312 " ' 数据回传客户端之前,加上这句,可避免乱码
response.write( " 我是中文 " )
response.end
% >
5.utf-8编码的Asp中,调用gb2312编码的javascript文件中的函数,发生中文乱码
简单解决方法:Asp中,在引用javascript文件的链接中加上charset=gb2312。
6. 解决utf-8编码下 echo 弹出javascript alert中文乱码解决方法
echo "<script>alert('删除失败:因已有".$num2."条子类别引用该类别.');</script>"; //会中文乱码
解决:echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
echo "<script>alert('删除失败:因已有".$num2."条子类别引用该类别.');</script>";
7.asp,在员工管理列表,输入姓名进行查询,对某个员工信息进行修改。修改后,返回本次查询的员工列表。由于查询姓名有中文字符,在修改后返回列表时,引起中文乱码。
假设查询的员工列表:http://…/StaffRoster.asp?…action=edit&spage=&skeywords=阿瓦隆
解决方法:在修改页面获取查询条件姓名时,skeywords=server.URLEncode(request(“skeywords”))
转换后的返回url:http://…/StaffRoster.asp?…action=edit&spage=&skeywords=%B0%A2%CD%DF%C2%A1
有可能修改页面进行修改操作后,返回url依然会乱码。
所以在修改操作中也需要,skeywords=server.URLEncode(request(“skeywords”))
但是这导致二次编码,返回到本页面也会导致又一次编码。
故正确做法:修改页面获取查询条件姓名时,skeywords=URLDecode(request(“skeywords”))
skeywords=server.URLEncode(request(“skeywords”))
修改操作时,skeywords=URLDecode(request(“skeywords”))
skeywords=server.URLEncode(request(“skeywords”))
附URLDecode
Function URLDecode(enStr)
dim deStr
dim c,i,v
deStr=""
for i=1 to len(enStr)
c=Mid(enStr,i,1)
if c="%" then
v=eval("&h"+Mid(enStr,i+1,2))
if v<128 then
deStr=deStr&chr(v)
i=i+2
else
if isvalidhex(mid(enstr,i,3)) then
if isvalidhex(mid(enstr,i+3,3)) then
v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))
deStr=deStr&chr(v)
i=i+5
else
v=eval("&h"+Mid(enStr,i+1,2)+cstr(hex(asc(Mid(enStr,i+3,1)))))
deStr=deStr&chr(v)
i=i+3
end if
else
destr=destr&c
end if
end if
else
if c="+" then
deStr=deStr&" "
else
deStr=deStr&c
end if
end if
next
URLDecode=deStr
end function
'判断是否为有效的十六进制代码
function isvalidhex(str)
dim c
isvalidhex=true
str=ucase(str)
if len(str)<>3 then isvalidhex=false:exit function
if left(str,1)<>"%" then isvalidhex=false:exit function
c=mid(str,2,1)
if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
c=mid(str,3,1)
if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
end function