
I hope that this post will be useful to many developers, because judging by the numerous threads on the Internet, the problem is quite frequent. The essence of the problem is the following: incorrect name of utf-8 encoding in nginx / apache settings. At the same time, the content returned by the server is perceived normally in all browsers except Internet Explorer.
Often, many developers, when configuring virtual hosts, copy settings from somewhere on the Internet or from other places. And at the same time, an error “migrates” to their settings. In the case of nginx, this is the directive:
charset utf8;
In the case of Apache, this is:
<Directory / path / to / site />
AddDefaultCharset UTF8
</ Directory>
So - there is no such encoding as
utf8 ! Correctly write
utf-8 (hyphenated). Most browsers (Firefox> = 3, Opera> = 9, Chrome> = 4, Safari> = 4) are loyal to specifying
utf8 as an encoding, and perceive the content given out correctly, but all versions of
Internet Explorer (including even the latest, 9th) ) instead of content give out "krakozyabry." Of course, this situation is easy to manage even without fixing the settings of the web server. For example, in the case of returning dynamic content using PHP, you can explicitly specify the encoding in the script itself:
header ( 'Content-type: text / html; charset = utf-8' );
Or use the following tag in HTML:
< meta http-equiv = "content-type" content = "text / html; charset = utf-8" />
And everything would be fine, but the situation becomes more complicated when using AJAX JavaScript tries to reach static content. An example using jQuery:
$ ( document ) .ready ( function () {
function print_r ()
{
// ...
}
$ .ajax ({
url: "/test.txt" ,
dataType: "text" ,
success: function (data, textStatus) {
$ ( '#res' ) .html (data);
},
error: function (jqXHR, textStatus, errorThrown) {
$ ( '#res' ) .html ( 'jqXHR:' + print_r (jqXHR) + '<br /> textStatus:' + textStatus + '<br /> errorThrown:' + print_r (errorThrown));
}
});
});
In this case, IE produces the following error:
{
jqXHR: {
readyState: 4,
status: 0,
statusText: 'error'
},
textStatus: 'error' ,
errorThrown: {
name: 'Error' ,
number: -1072896658,
description: 'Could not complete the action. Error c00ce56e. ' ,
message: 'Could not complete the action. Error c00ce56e. '
}
}
This JS error is quite nontrivial. And if users, who have already encountered such a thing, immediately understand what is the matter, then those who encounter such a thing for the first time may be perplexed and take a long time to figure out the reason.
PS As it was rightly noted in the comments, when accessing MySQL, the query is used to indicate in which encoding to access the database:
SET NAMES utf8
And in general, wherever there is a reference to the utf-8 encoding, for example, when creating a table:
CREATE TABLE `some_table` (...) ENGINE = innoDB DEFAULT CHARSET = utf8
The encoding is utf8, not utf-8. That is, in this case, the recording goes WITHOUT a hyphen, which makes even more cognitive dissonance in understanding what is happening ...