3

first of all it is a userscript and I can't change the server-side encoding.

My problem is that when using encodeURIComponent() for encoding POST params (later sent via xhr.setRequestHeader), the characters are encoded in utf-8, but the server needs to receive iso-8859-1 data. Is there an alternative to encodeURIComponent() that would encode in iso-8859-1 ?

.

To make sure you understand, here is an exemple:

A classic form on the website send é like this: yournewmessage:%E9

Ajax via xhr.send('yournewmessage='+encodeURIComponent('é')) sends this: yournewmessage:%E9%80%80

The server needs the former. Thanks to anyone who can help me.

  • 1
    Not sure this makes much sense. URIs use UTF8 by specification, as defined in RFC2396. – Polynomial Dec 17 '11 at 14:40
  • @Polynomial Maybe it doesn't make much sense, but it is sometimes needed and web browsers know how to do it. – Alexandre Dieulot Dec 17 '11 at 14:45
  • 1
    Only solution I can think of is to convert each *byte* (not char) in the string to a hex representation and upload that. There's a great answer about getting encoding-angostic byte arrays from strings in JS here: http://stackoverflow.com/questions/1240408/reading-bytes-from-a-javascript-string – Polynomial Dec 17 '11 at 14:49

1 Answers1

0

So, I’ve since figured out this problem. What I did was searching for an equivalence between utf-8 and iso-8859-1, what I found was between utf-8 and cp1252 (Windows-1252) so there are two conversions, utf-8 to cp1252 and cp1252 to iso-8859-1 (these two having a lot of similarities)

http://pastebin.com/jTDqR2PQ

Ugly code, comments left in French, and unelegant solution, but I feel bad seeing this question unansered while I actually found a solution that works.