Ajax request encoding

340 views
Skip to first unread message

Joao Paulo

unread,
Oct 21, 2008, 3:27:50 PM10/21/08
to Prototype & script.aculo.us
Hi all,

I'm using prototype 1.6.0.2 and I'm getting in trouble when sending an
ajax request and getting the submitted values from a charset other
than UTF-8. The code:

<form action="my_url" accept-charset="ISO-8859-1" id="form1">
<input type="text" name="some_name" value="some ISO-8859-1 only
chars"
</form>
$('form1').request({encoding: 'ISO-8859-1'})

I'm getting the result in UTF-8, but it should be in ISO-8859-1. I'm
converting the string from one charset to another with java, but I
think this ins't the expected result.

Is this normal? Or is an issue with javascript's encodeURIComponent
function?


Thanks a lot. :D

Baglan

unread,
Oct 21, 2008, 11:12:00 PM10/21/08
to Prototype & script.aculo.us
Hello!

The 'encoding' option seems not to be doing much. Request is sent in
UTF-8 and response encoding is determined by the encoding conveyed in
the HTTP header of the response.

In practical terms, you can skip the 'encoding' parameter, expect data
in UTF-8 on the server and set response content type and charset to:

text/html; charset=iso-8859-1

- Baglan

João Paulo Mafra

unread,
Oct 22, 2008, 8:23:08 AM10/22/08
to prototype-s...@googlegroups.com
Hi Baglan,

I'm already doing that. However, in the server I must convert from one charset to another programmaticaly.

The problem seems to be the use of "encodeURIComponent" function by prototype:

Mozilla Developer Core Javascript Guide wrote:
encodeURIComponent
 
Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.

Replacing all occurences of "encodeURIComponent" function by "escape" function solved the problem. The question is whether using "escape" is worth for that.

Mozilla Developer Core Javascript Guide wrote:
escape and unescape
 
The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII
string for the specified hexadecimal encoding value.


The article on http://xkr.us/articles/javascript/encode-compare/#ref-js-ns shows the differences between some functions to encode strings.

Is there any chance to enhance prototype to use the correct function based on an "encoding" parameter?


Thanks a lot. (sorry my bad english) :D


2008/10/22 Baglan <bagla...@gmail.com>

João Paulo Mafra

unread,
Oct 22, 2008, 8:48:42 AM10/22/08
to prototype-s...@googlegroups.com
(I'm resending my last message in plain text after viewing it on
google groups, which is in html and with formatting errors)

Hi Baglan,

I'm already doing that. However, in the server I must convert from one
charset to another programmaticaly.

The problem seems to be the use of "encodeURIComponent" function by prototype:

Mozilla Developer Core Javascript Guide

(http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Functions:encodeURIComponent):


encodeURIComponent
Encodes a Uniform Resource Identifier (URI) by replacing each instance
of certain characters by one, two, or three escape sequences
representing the UTF-8 encoding of the character.

Replacing all occurences of "encodeURIComponent" function by "escape"
function solved the problem. The question is whether using "escape" is
worth for that.

Mozilla Developer Core Javascript Guide

(http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Predefined_Functions:escape_and_unescape_Functions):

Reply all
Reply to author
Forward
0 new messages