Asp,jquery,ajax Chinese garbled solution

Source: Internet
Author: User
>1, as long as in the AJAX data submitted, if the page encoding is not utf-8, should be the data submitted to encode, JS encoding function for escape ()
2, after the server-side page to receive data decoding, and then the data related to the processing and then coding
3, return to the client and then decode
4, if not submit data, but directly from the server to obtain data, that directly in the server page set response.charset= "gb2312" can be, no more coding and decoding
In VBScript corresponds to the Escape () and unescape () function program code in JS <%
' is equivalent to escape () in JavaScript
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
' is equivalent to unescape () in JavaScript
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&AMP;CHRW (CLng ("&h" &mid (str,x+2,4))
Str=mid (STR,X+6)
Else
VBSUNESCAPE=VBSUNESCAPE&AMP;CHR (CLng ("&h" &mid (str,x+1,2))
Str=mid (str,x+3)
End If
X=instr (str, "%")
Loop
Vbsunescape=vbsunescape&str
End Function
%> for a demo:

Client page: client.html

Program Code <script>
The post of jquery
$.post
(
' Server.asp ',
{
ACT: ' Dosubmit ',
Username:escape (' West Building Cold Month '),//coding
WebSite: ' www.chinacms.org '
},
function (data)
{
Alert (unescape (data));//decode the returned data
}
);

</script>
Server Side page: server.asp
Program Code <%
response.charset= "gb2312"
Dim Username,website
If Request.Form ("Act") = "Dosubmit" Then
Username=request.form ("UserName")
WebSite =request.form ("WebSite")

' Decoding on the server side
Username=vbsunescape (UserName)//decoding

' Process data
'---Omit the data Processing Section

' Data processing after output, first with vbsescape () encoding
' By www.chinacms.org
Response.Write Vbsescape (UserName)
End If
%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.