今天做程序,发现jQuery无刷新ajax,发现一个问题传入编码为汉字,只是接收页面怎么处理都是乱码; 最终解决办法,先在传之前进行编码,然后获取后再进行解码! 所用到的函数: <% '与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()编码 $("#Input").click(function(){ $.ajax({ type: "POST", url: "LfCall.asp", contentType: "application/x-www-form-urlencoded; charset=gb2312", data: {Result:'Save',CallName:escape($("#Name").val()),CallPhone:escape($("#Phone").val()),CallContent:escape($("#Content").val())}, success: function(data,textStatus){ $("#lfcallboxcontent").html(data); } }); });
解码使用的是asp中的VbsEscape(str)来解码
信息提交成功:<br /> <%=VbsUnEscape(Request.Form("CallName"))%><br /> <%=Request.Form("CallPhone")%><br /> <%=VbsUnEscape(Request.Form("CallContent"))%>
|