0

I'm having a problem with this function in specific:

$.post('classes/processPage.asp', {
  param1: $('#hdnValue').val()
}

When I test this value on the processPage, it gives me wrong characters.

I've tried to include this:

$.ajaxSetup({contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1"});

But it didn't work.

For information the encoding of the files (classic ASP pages) are ANSI, but I also have tried the UTF-8.

I have this meta tag included in the first page:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

And this code in the processPage:

Response.Charset = "ISO-8859-1"

What am I missing?

Marco
  • 1,279
  • 1
  • 15
  • 26
  • This may help it applies to any encoding not just `UTF-8` - [Answer to Classic ASP - How to convert a UTF-8 string to UTF-16?](http://stackoverflow.com/a/25646123/692942). Especially look at the list of things the **client** and **server** need for encoding to work flawlessly. Also this (again works with any encoding, the principles detailed are **exactly** the same) - [Answer to convert utf-8 to iso-8859-1 in classic asp](http://stackoverflow.com/a/17680939/692942) – user692942 Oct 09 '14 at 13:39
  • It's strange cause this error only happens when I send the data through $.post... If I send using the form post, the characters goes right. – Marco Oct 09 '14 at 14:31
  • In that case your sending the wrong encoding with the `$.post()`. Use `<% Response.Write "CodePage = " & Response.CodePage %>` in your page with the `$.post()` on it to check that the encoding is. – user692942 Oct 09 '14 at 14:42
  • It's returning 1252, that I guess it's the related codepage for ISO-8859-1. – Marco Oct 09 '14 at 16:48
  • The $.post() always send the UTF-8 charset, so I just decoded this string in server-side using a function. – Marco Oct 09 '14 at 18:26
  • No don't do that just pass the correct `charset` to `$.post()` or save your `processPage.asp` using `UTF-8` and use `Response.CodePage = 65001`!! Don't try and cut corners, using so-called decoding functions will cause you nothing but grief in the long run. – user692942 Oct 09 '14 at 20:20

1 Answers1

-1

I saw this at jQuery's page:

"When sending data to the server, use this content-type. Default is "application/x-www-form-urlencoded", which is fine for most cases. If you explicitly pass in a content-type to $.ajax() then it'll always be sent to the server (even if no data is sent). Data will always be transmitted to the server using UTF-8 charset; you must decode this appropriately on the server side"

So I decoded the string in server-side using this function:

'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
Marco
  • 1,279
  • 1
  • 15
  • 26
  • I'm sick of explaining this in post after post **do not do this**!! – user692942 Oct 09 '14 at 20:22
  • The fix is make sure that both the page containing the `$.post()` code and the page you are posting to are both saved as `UTF-8` encoding (as per the instructions in the links provided). Anything else is just going to lead to problems and upside down question marks `¿¿¿¿` ;). – user692942 Oct 09 '14 at 20:43
  • Yeah I read and I thought about change the encoding to UTF-8 in both pages... but the whole project is using ANSI encoding and ISO-8859-1. It would be out of the pattern, and I'm just maintaining.... Anyway, thanks for the links and the reply. o/ – Marco Oct 10 '14 at 12:43
  • The fact you need to convert from `UTF-8` means you are not sending the POST data using the expected charset. You need to correct this, try inspecting your post using fiddler to see what charset the POST request get sent with. The ajax methods in jQuery are capable of posting data using character sets other then `UTF-8` the warning is there to point out what is recommended by them and the w3c. At the moment what your doing is far worse then that by manually attempting to convert the encoding, the IIS server should be doing this for you. – user692942 Oct 12 '14 at 14:53
  • I've tried to send the data in the ISO-8859-1 charset through the ajaxSetup method, but with no success. The best way is convert those pages to UTF-8. – Marco Oct 13 '14 at 15:57
  • Try using `$.ajax()` instead of `$.post()` and set the charset using that instead of `$.ajaxSetup()`. Also when setting the charset make sure you use the [correct codepage](http://msdn.microsoft.com/en-us/library/aa288104(v=vs.71).aspx) name. Should be using `iso8859-1`, `iso_8859-1` or `windows-1252`. Not sure `ISO-8859-1` is recognised by IIS, has to be as defined in that list. – user692942 Oct 13 '14 at 16:00