NYCPHP Meetup

NYPHP.org

[nycphp-talk] AJAX and character encoding

Mikko Rantalainen mikko.rantalainen at peda.net
Tue May 9 04:05:05 EDT 2006


Marc Antony Vose wrote:
> At 10:24 AM -0400 5/8/06, Adam Maccabee Trachtenberg wrote:
>> On Mon, 8 May 2006, tedd wrote:
>>
>>>  Correct me if I'm wrong, but if you are simply injecting code into a
>>>  static web page, isn't it the <meta... utf-8> tab of the page that
>>>  sets the charset?
>>>
>>>  Ajax simply prompts a php routine to prepare stuff for ajax to take
>>>  back an inject into whatever you want between html tags, right? At
>>>  least that's the way I'm doing it -- but, I'm simple that way.
>> Without doing any research, I would also guess the setting the charset
>> via a meta or HTTP header should fix this.
>>
>> If not, does you server have the iconv() extension enabled? That lets
>> you convert between arbitrary character sets.
> 
> To clarify this for everyone: the character encoding of the page is 
> in windows-1252.  Therefore, in my opinion, the browser ought to 
> display all the content in the character set.
> 
> But, in researching AJAX, I've found that the httprequest object 
> defaults all communication to UTF-8, no matter whether you set 
> character encoding on it with a PHP header or not.  Most browsers 
> seem to detect this and display the proper characters.

AJAX uses XMLHttpRequest() which uses XML. And XML uses UTF-8 unless 
  transportation layer (HTTP) or the XML declaration say otherwise.

If I've understood the problem correctly we have following situation:

1) HTTP server sends a text/html document X to browser with 
windows-1252 encoding
2) X contains javascript which uses XMLHttpRequest (a.k.a. AJAX) to 
query another web server Y.
3) Y returns XML document Z with HTTP without specifying header 
Content-Type: application/xml; charset=windows-1252 and without XML 
declaration such as <?xml version="1.0" encoding="windows-1252"?>.
4) X fails to correctly display results from Z because the browser 
"incorrectly" uses UTF-8.

If this is the case, the only way to fix the problem is to fix Y so 
that it sends correct HTTP headers (or XML declaration but HTTP 
headers are more important to get right).

The other reason for failing could be that the browser doesn't 
understand the non-standard character set name "windows-1252". Try 
to use "iso-8859-1" instead which is almost similar encoding and 
should be well supported.

-- 
Mikko



More information about the talk mailing list