我特定地遇到了这个功能的问题:
$.post('classes/processPage.asp', {
param1: $('#hdnValue').val()
}
当我在processPage上测试这个值时,它给了我错误的字符。
我试图将其包括在内:
$.ajaxSetup({contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1"});
但它没有用。
有关信息,文件(经典ASP页面)的编码是ANSI,但我也尝试过UTF-8。
我在第一页中包含了这个元标记:
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
这个代码在processPage中:
Response.Charset = "ISO-8859-1"
我错过了什么?
答案 0 :(得分:-1)
我在jQuery的页面上看到了这个:
“将数据发送到服务器时,请使用此内容类型。默认为”application / x-www-form-urlencoded“,这在大多数情况下都适用。如果您明确地将内容类型传递给$。 ajax()然后它将始终发送到服务器(即使没有数据发送)。数据将始终使用UTF-8字符集传输到服务器;您必须在服务器端适当地解码它“
所以我使用这个函数在服务器端解码了字符串:
'DecodeUTF8
' Decodes a UTF-8 string to the Windows character set
' Non-convertable characters are replace by an upside
' down question mark.
'Returns:
' A Windows string
function DecodeUTF8(s)
dim i
dim c
dim n
i = 1
do while i <= len(s)
c = asc(mid(s,i,1))
if c and &H80 then
n = 1
do while i + n < len(s)
if (asc(mid(s,i+n,1)) and &HC0) <> &H80 then
exit do
end if
n = n + 1
loop
if n = 2 and ((c and &HE0) = &HC0) then
c = asc(mid(s,i+1,1)) + &H40 * (c and &H01)
else
c = 191
end if
s = left(s,i-1) + chr(c) + mid(s,i+n)
end if
i = i + 1
loop
DecodeUTF8 = s
end function