当前位置: 移动技术网 > IT编程>开发语言>Asp > Asp中使用JQuery的AJAX提交中文乱码解决方法

Asp中使用JQuery的AJAX提交中文乱码解决方法

2017年12月08日  | 移动技术网IT编程  | 我要评论

客户端页:client.html

复制代码 代码如下:

<script>
    //jquery的post
    $.post
    (
        'server.asp',
        {
            act:'dosubmit',
            username:escape('移动技术网'),//进行编码
           website:'www.jb51.net'
        },
        function(data)
        {
            alert(unescape(data));//对返回数据进行解码
        }
    );   
</script>

服务器端页:server.asp

复制代码 代码如下:

< %
response.charset="gb2312"
dim username,website
if request.form("act")="dosubmit" then
username=request.form("username")
website =request.form("website")
 
'在服务器端解码
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() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串。

可以使用 unescape() 对 escape() 编码的字符串进行解码。

其实asp中这两个函数也是起作用的,居然很多asp网站上没有进行介绍。

要不然只能像上面那样写函数进行解码编码了。复杂且性能不好。

上面的服务器端页:server.asp可以写成:

asp中的unescape() 与 escape() 函数

复制代码 代码如下:

< %
response.charset="gb2312"
dim username,website
if request.form("act")="dosubmit" then
username=request.form("username")
website =request.form("website")
 
'在服务器端解码
username=unescape(username)//解码
 
'处理数据
'---省略数据处理部分
 
'数据处理后输出,先用vbsescape()编码
response.write escape(username)
end if
%>

这样就简单多了。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网