September 5, 2013

Apache, CGI::Ajax, and UTF-8

I just spent the day figuring out why, oh why, my Apache server wouldn't do as it was told. No matter what changes were made to the httpd.conf file, it kept serving up a page in ISO-8859-1 rather than UTF-8.

I tried all sorts of stuff suggested on the net. In the end, the server was configured for UTF-8, the .html file was saved in UTF-8, that file had both an <?xml version="1.0" encoding="UTF-8"?> tag (with appropriate tags for strict xhtml) and a <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> tag. None of this made a difference. I even made a change to my installed Samba server's configuration in case that was mucking things up. But there was no joy.
If this post doesn't solve your UTF-8 problem,
check out The Mother Of All UTF-8 Checklists!

As I was falling asleep for lack of coffee, a dim light went on... The page being served was actually generated by a Perl script that sucked in the html file and used a CGI::Ajax object to spit out the page with Ajax-enabling code added. I'd put together that part of the Perl script a couple months ago and just noticed the odd encoding today, so maybe the problem was there.

Checking the CGI::Ajax page, the end of the first example is pretty clear...
print $pjx->build_html($cgi,\&Show_HTML,{-charset=>'UTF-8', -expires=>'-1d'});
After getting some more coffee, I dropped {-charset=>'UTF-8'} into my build_html method call, pulled up the page on Firefox, checked the page info, and... Voila! It's UTF-8. Finally.

The CGI::Ajax module is awesome and solving the problem is great, but I can't believe I wasted a day figuring this out. Here's hoping this post saves someone out there from the same fate.

No comments:

Post a Comment