Combining foreign characters, JavaScript and calls to the server in a JSON format sounds like trouble — and it is. But this time Safari is causing the problems instead of IE. If encoding is not specified in the headers, characters like Å, Ä and Ö will not display properly. Now, if you have come across this problem, I’m sure you have seen the solution presented by macdiggs, which seems to have fixed the problem by adding the proper encoding in the header.

header("Content-type:text/html; charset=utf-8");

Because the programming gods don’t like me, this didn’t do the trick for me. Looking over my code, all encoding is properly set. The HTML is:

header("Content-Type: text/html; charset: utf-8"); 

And the CSS:

header("Content-Type: text/css; charset: utf-8");

And so on with all other file types. The permanent solution for me was to set this in the .htaccess file.

AddDefaultCharset UTF-8

I suspect that somewhere in the application the encoding is being transferred incorrectly, but all tools I can use (Firebug, page load info) say otherwise. Even changing the default charset in Safari preferences didn’t fix the problem. If you’re running into something similar, know that the issue is with encoding. If fixing it on a page level doesn’t work then the .htaccess change may be what you need.

UPDATE : Turns out I’m a goof (and Kevin) and missed that the charset is set with an equals sign and not a colon. Ignore the above. Thanks Kurt!

HTML Form Builder
Ryan Campbell

Safari and Ajax with UTF-8 Encoding by Ryan Campbell

This entry was posted 2 months ago and was filed under Notebook.
You can follow comments on this entry by subscribing to the RSS feed. Comments are currently closed.

· 15 Comments! ·

  1. Kurt McKee · 2 months ago

    This wouldn’t happen to be an issue with you using colons in place of the equals sign for charset, would it? I don’t know what the syntax is, but yours appears to differ from macdiggs’.

  2. Ryan Campbell · 2 months ago

    Wow, that did fix it. Thanks for catching that. I did try that originally, but it appears the way we use ob_start requires different placement. That’s app specific though. I’m still not sure why everything still checked out as UTF-8, but thanks for solving my oversight.

  3. Jehaih · 2 months ago

    And the t in type in the first example should be capitalized. It’s Content-Type not Content-type….. though most app servers/browsers seem to treat them equally.

  4. Maciej Lebkowski · 2 months ago

    Acording to this: http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 The header-check should be case-insensitive.

    There is, however, no obligation to use whitespace between the collon and the header value (spec mentions “any amount”, i.e., zero is fine).

  5. adsl · 2 months ago

    just blame safari, thats always a good solution ;-)

  6. adsl · 2 months ago

    alhtough im a great fan of safari, sorry forgot to add!

  7. Luis · 2 months ago

    I saw an article about this trouble on your site a few weeks ago, searching on google. I have to thank you dude… it really helped a lot. I had some troubles with UTF-8. Changed the mysql collaction, default charset, php default charset, apache’s charset, add the headers on my ajax object, etc. After all that I discovered that escape() worked just fine too :)

  8. Philip Tellis · 2 months ago

    Putting it into your apache config files is the best solution, because it works for all scripts and for static HTML files.

  9. Geld Lenen · 2 months ago

    I keep on having probs with Russian characters. We wanna translate our site to Russian but still character issues. Has this something to do also with server config? Perhaps get a Russian host?

    BTW. What a sh^tload of spam here!

  10. wesley · 2 months ago

    Shouldn’t you be updating the article now that it is known that the fault was your own code.. Thanks.

  11. Niyaz PK · 2 months ago

    Everyone really needs a hug. Its OK for one time. To forgive is divine. :)

  12. Handan Daily · 2 months ago

    Everyone needs a hug.Putting it into your apache config files is the best solution, because it works for all scripts and for static HTML files.

  13. Handan Daily · 2 months ago

    Everyone needs a hug. Chinese: http://www.cnblogs.com/kaixin110/archive/2008/03/13/1103405.html

  14. Opony · 1 month ago

    I really like this post. I agree with you. In my country, a lot of companies still insist on having websites designed according to their taste and not their users. Your post will really help me open their eyes and accept the reality.

  15. Shelton Wooten · 1 month ago

    petrographic tortonian dendrodus sisterless orthobrachycephalic biomechanics achromatically infecter Cuba and Its Music http://www.purrbalds.com