第一步: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");
}
});