12

I have an Apache server which is serving out static JSON files. The Content Type is correctly being represented in the header with an Content-Type: application/json header.

How do I add the correct Charset to the header?

I have the following line in my httpd.conf:

AddDefaultCharset utf-8

but even with this line, Apache does not set the charset in the header.

This is important, because my JSON files have some data in French, and the Accents are not being properly read in the JavaScript Code.

So how do I set proper Headers for JSON in Apache?

Giacomo1968
  • 3,522
  • 25
  • 38
Devdatta Tengshe
  • 583
  • 2
  • 4
  • 13
  • How are your JSON-files encoded? Because JSON HAS to be either UTF-8, UTF-16 or UTF-32 anything else is not allowed and just sending a different Charset will not automagically convert the files. – lsmooth Mar 13 '14 at 11:16
  • The JSON is encoded in UTF-8 – Devdatta Tengshe Mar 13 '14 at 14:02
  • @lsmooth The file being encoded with UTF-8 is not enough in some circumstances, especially involving IE9 (possibly earlier, too); see [here](http://rob.conery.io/2011/10/24/a-fun-little-json-murder-mystery-with-ie9/). NSFW language, unsurprisingly :-). Personally, I've encountered issues with jquery-datatables and JSON without charset headers when testing with IE9. – JK Laiho Mar 18 '15 at 13:15

2 Answers2

12

You can use the following code in your httpd.conf if you have access, or even your .htaccess , to force Apache, to send an UTF-8 encoding header.

AddDefaultCharset utf-8
AddType 'application/json; charset=UTF-8' .json
Devdatta Tengshe
  • 583
  • 2
  • 4
  • 13
1

You can add this to httpd.conf

AddCharset utf8 .json
Giacomo1968
  • 3,522
  • 25
  • 38