Ajax calls and characters encoding (accents)


#1

Hello, my application controller defines

before_filter :set_charset
def set_charset
@headers[“Content-Type”] = “text/html; charset=ISO-8859-1”
end

Everything is fine and accents are rendered correctly in the browser.

However, when I call a controller/action from Ajax (Prototype), the
charset is not taken in account and accents are garbage displayed (’?’
sign)

I’ve tried to put
@headers[“Content-Type”] = “text/html; charset=ISO-8859-1”
in the partial view rendered from the ajax call
and/or
in the the controller code itself

No way, still can’t have correctly rendered accented chars

How can I do ?

I’ve found a thread in the forum stating that Prototype uses only utf-8
? I certainly don’t want to migrate to utf-8 now. Is it true ?

Thanks


#2

Here’s my code:

def set_charset
if request.xhr?
@headers[“Content-Type”] = “text/javascript; charset=utf-8”
else
@headers[“Content-Type”] = “text/html; charset=utf-8”
end
end

HTH


#3

yes Steve, but the problem is that I don’t want to switch to utf-8 now.
In fact, I even did a try without any response header BUT just with the
strings utf-8 encoded by RadRails and it worked out of the box.

I tried with encoding special characters as html entities. It works when
used directly into the partial’s view but not when the string is
returned by a ruby method.

eg

  1. model
    class ContactEvent < ActiveRecord::Base
    def self.evt_modes
    return {‘TEL’ => ‘Téléphone’, ‘RDV’ => ‘Rendez-vous’,
    ‘MISC’ => ‘Autre’}
    end
    end

  2. partial
    This is a special char ‘é’
    Mode <%= select ‘contact_event’, ‘how’, ContactEvent.evt_modes.invert %>

When rendered, the é is rendered correctly in “This is a special
char” but not at all in the items on the select list. I really don’t
understand why.

Any idea ??


#4

steve ross wrote:

A different facet of this issue is giving me trouble too. I’m not
convinced I completely understand it.

Questions:

  1. Have you tried calling the Ajax method via GET? (http://myserver/
    mycontroller/myhandler)? What were the results?

Got it ! The error is due to the fact that é is transformed to
&eacute;

But why ? And how to change that ? Is it a bug ?


#5

A different facet of this issue is giving me trouble too. I’m not
convinced I completely understand it.

Questions:

  1. Have you tried calling the Ajax method via GET? (http://myserver/
    mycontroller/myhandler)? What were the results?

  2. Have you tried GET via telnet to get the actual header info to see
    what the browser might be reacting improperly to?

  3. Are you convinced that the character codes coming from the partial
    should render correctly in the characterset you’ve declared? You can use
    logger.debug to hex dump the codes of the offending characters.

Note: IE/Win does heuristic analysis of characters in the absence of
headers and will try to figure out the right character set. If this is
breaking you, try Moz or Opera to see if the problem repros.

You’d think after this long, the software development world (emphasis on
WORLD) would be able to figure out some happy way to make characters
render reliable across disparate viewing platforms.