ASP, jquery, and Ajax Chinese garbled Solutions

Source: Internet
Author: User

The aspweb server does not support response. charset.

So use Encoding

 

> 1. when data is submitted in Ajax, if the page encoding is not UTF-8, the submitted data should be encoded. The JS encoding function is escape ()
2. decode the received data on the server page, process the data, and encode the data.
3. Return to the client before Decoding
4. If no data is submitted, but the data is directly obtained from the server, set response. charset = "gb2312" on the server page without encoding or decoding.
The values in VBScript correspond to the escape () and Unescape () functions in JS respectively.

Program code <%
'Is equivalent to escape () in Javascript
Function vbsescape (STR)
Dim I, S, C,
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 ()
Elseif A> = 16 and a <256 then
S = S & "%" & hex ()
Else
S = S & "% u" & hex ()
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 & 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
%> Here is a demo:

Client page: client.html

Program code <SCRIPT>
// Jquery post
$. Post
(
'Server. asp ',
{
Act: 'dosubmit ',
Username: escape ('West floor cold month'), // encode
Website: 'www .chinacms.org'
},
Function (data)
{
Alert (Unescape (data); // decodes the returned data
}
);

</SCRIPT> Server 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) // decode

'Process data
'--- Omitting the data processing part

'Output after data processing, first encoded with vbsescape ()
'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.