Ver Mensaje Individual
  #2 (permalink)  
Antiguo 07/09/2011, 03:22
Pikus
 
Fecha de Ingreso: febrero-2010
Mensajes: 22
Antigüedad: 14 años, 3 meses
Puntos: 0
Respuesta: Problemas de especificación de charset

Tras continuar pateándome Google llegué a la conclusión de que no había manera de decirle a Ajax que envíe los datos en otra codificación que no sea UTF8, por ello decidí que lo mejor sería cambiar de UTF8 a iso-8859-1 directamente desde ASP. Como no hay una función concreta que lo haga, encontré algo efectivo y que me funcionó:

Fuente: http://www.codetoad.com/asp_utf8.asp
Código ASP:
Ver original
  1. <%
  2. Option Explicit
  3.  
  4. ' Simple Functions to convert the first 256 characters
  5. ' of the Windows character set from And to UTF-8.
  6.  
  7. ' Written by Hans Kalle for Fisz
  8. ' http://www.fisz.nl
  9.  
  10. 'IsValidUTF8
  11. '  Tells If the string is valid UTF-8 encoded
  12. 'Returns:
  13. '  true (valid UTF-8)
  14. '  false (invalid UTF-8 or not UTF-8 encoded string)
  15. Function IsValidUTF8(s)
  16.   Dim i
  17.   Dim c
  18.   Dim n
  19.  
  20.   IsValidUTF8 = false
  21.   i = 1
  22.   Do While i <= len(s)
  23.     c = asc(mid(s,i,1))
  24.     If c And &H80 Then
  25.       n = 1
  26.       Do While i + n < len(s)
  27.         If (asc(mid(s,i+n,1)) And &HC0) <> &H80 Then
  28.           Exit Do
  29.         End If
  30.         n = n + 1
  31.       Loop
  32.       Select Case n
  33.       Case 1
  34.         Exit Function
  35.       Case 2
  36.         If (c And &HE0) <> &HC0 Then
  37.           Exit Function
  38.         End If
  39.       Case 3
  40.         If (c And &HF0) <> &HE0 Then
  41.           Exit Function
  42.         End If
  43.       Case 4
  44.         If (c And &HF8) <> &HF0 Then
  45.           Exit Function
  46.         End If
  47.       Case Else
  48.         Exit Function
  49.       End Select
  50.       i = i + n
  51.     Else
  52.       i = i + 1
  53.     End If
  54.   Loop
  55.   IsValidUTF8 = true
  56. End Function
  57.  
  58. 'DecodeUTF8
  59. '  Decodes a UTF-8 string to the Windows character set
  60. '  Non-convertable characters are replace by an upside
  61. '  down question mark.
  62. 'Returns:
  63. '  A Windows string
  64. Function DecodeUTF8(s)
  65.   Dim i
  66.   Dim c
  67.   Dim n
  68.  
  69.   i = 1
  70.   Do While i <= len(s)
  71.     c = asc(mid(s,i,1))
  72.     If c And &H80 Then
  73.       n = 1
  74.       Do While i + n < len(s)
  75.         If (asc(mid(s,i+n,1)) And &HC0) <> &H80 Then
  76.           Exit Do
  77.         End If
  78.         n = n + 1
  79.       Loop
  80.       If n = 2 And ((c And &HE0) = &HC0) Then
  81.         c = asc(mid(s,i+1,1)) + &H40 * (c And &H01)
  82.       Else
  83.         c = 191
  84.       End If
  85.       s = left(s,i-1) + chr(c) + mid(s,i+n)
  86.     End If
  87.     i = i + 1
  88.   Loop
  89.   DecodeUTF8 = s
  90. End Function
  91.  
  92. 'EncodeUTF8
  93. '  Encodes a Windows string in UTF-8
  94. 'Returns:
  95. '  A UTF-8 encoded string
  96. Function EncodeUTF8(s)
  97.   Dim i
  98.   Dim c
  99.  
  100.   i = 1
  101.   Do While i <= len(s)
  102.     c = asc(mid(s,i,1))
  103.     If c >= &H80 Then
  104.       s = left(s,i-1) + chr(&HC2 + ((c And &H40) / &H40)) + chr(c And &HBF) + mid(s,i+1)
  105.       i = i + 1
  106.     End If
  107.     i = i + 1
  108.   Loop
  109.   EncodeUTF8 = s
  110. End Function
  111. %>

Al llamar a esa función dentro de mi código se acabaron los problemas de codificación:

Código ASP:
Ver original
  1. ' Transformamos la localidad de UTF8 a iso-8859-1 si está codificada en UTF8
  2. ' ============ IMPORTANTE ============
  3. ' ************************************
  4. ' Hay que decodificarla así porque el string se recibe en UTF8 siendo de origen Windows 1252 y provoca caracteres raros
  5. ' como es UTF8 valid hay que decodificarlo para que se transforme en Windows 1252
  6. ' En caso de ser invalid es porque nos lo han enviado directamente en Windows 1252 (caso de IE8) que es lo que nos interesa, no lo tranformamos
  7. If IsValidUTF8(Request.QueryString("loc")) Then
  8.     localidad = decodeUTF8(Request.QueryString("loc"))
  9. Else
  10.     localidad = Request.QueryString("loc")
  11. End If

Un saludo y espero que le sirva a alguien.