My first assumption was that the encoding of the file was incorrect and when I changed it to UTF-8 everything worked fine. However, it should work with ISO-8859-1 as well so I changed the encoding back and started sniffing the HTTP headers.
It turns out that neither IIS nor Apache include the encoding of js files in the HTTP response headers. My guess is that when the file was loaded through the HTML script tag the browser did some heuristics to figure out that the file was actually ISO-8859-1. The same heuristics does not apply to the XMLHttpRequest and it assumes that the file is UTF-8 which it obviously isn’t.
So to get this to work you need to set:
I guess the issue is that the web servers do not know that js files are text files and therefore don’t bother with the encoding. Does anyone know how to configure web servers to automatically insert the correct charset in the HTTP header?
This is a real world problem that more and more people will run into as the world moves more and more towards using XMLHttpRequest to load JSON and other text based data formats.