Special characters display as ? marks
I have some special characters here:
http://209.141.56.244/test/char.phpbut when I grab this file via ajax here, they show up as back ? marks:
http://209.141.56.244/test/char.htmlThese 开发者_Python百科characters should be "ISO-8859-1 Western" but switching my browser encoding to any of the options don't help.
Why is this and how can I fix it?
Your server sends the Ajax response as text/html
, this makes the browser assume UTF-8 (which is the default), but your data itself is not UTF-8.
Send the Ajax response as text/html; Charset=Windows-1252
(you're not really using ISO-8859-1) and it should work.
header('Content-Type: text/html; Charset=Windows-1252');
PS: Switching the browser to another charset does not help because this affects the page itself only. Subsequent Ajax responses are still decoded according to their respective headers.
Your page return text/html
as Content-Type
, so the browser (and the ajax script) interpret them with a default encoding given by the current context.
In php you can force the encoding using the header function for the html version you should use apache configuration files (assuming you're using apache, otherwise see your webserver doc).
http://www.w3.org/International/O-HTTP-charset says
It is very important to always label Web documents explicitly. HTTP 1.1 says that the default charset is ISO-8859-1. But there are too many unlabeled documents in other encodings, so browsers use the reader's preferred encoding when there is no explicit charset parameter.
from the same page
Apache. This can be done via the AddCharset (Apache 1.3.10 and later) or AddType directives, for directories or individual resources (files). With AddDefaultCharset (Apache 1.3.12 and later), it is possible to set the default charset for a whole server. For more information, see the article on Setting 'charset' information in .htaccess.
精彩评论