jquery ajax asp 中文乱码

12 篇文章 0 订阅
1 篇文章 0 订阅

第一步:asp 中增加函数


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

第二步:在asp中发送返回值到javascript的时候,需要使用第一步建立的函数对要返回的内容进行编码

dl = "要返回的内容"

Response.Write(VbsEscape(dl))

第三步:在javascript中将接受到的内容进行解码(使用函数unescape)


 $.ajax({
	type:"GET",
	url:"getNewPageData.asp?action=newPageData",
	data: str, //要发送到后台的数据为 搜索关键字keyword和要显示的数据为第newPage页面
	timeout:300000,
	dataType : "JSON",
	success:function(result){
                      if ( result){
			result= unescape(result);//解码接收到的返回值
			        createTable(result,270,270);                  
                       }
			},
	error:function(){
			alert("error");
	}
	});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值