javascript 未声明 HTML 文档的字符编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32479888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:20:51  来源:igfitidea点击:

The character encoding of HTML document was not declared

javascripthtmlnode.js

提问by Rohan

I am serving an HTML page from a Node.js server and get this error on the browser console -

我正在从 Node.js 服务器提供 HTML 页面,并在浏览器控制台上收到此错误 -

The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.

未声明 HTML 文档的字符编码。如果文档包含来自 US-ASCII 范围之外的字符,则文档将在某些浏览器配置中呈现为乱码。页面的字符编码必须在文档或传输协议中声明。

I know this question has been asked before, but most, if not all, of the solutions suggest adding this line to the HTML file -

我知道以前有人问过这个问题,但是大多数(如果不是全部)解决方案都建议将此行添加到 HTML 文件中 -

<meta content="text/html; charset=utf-8" http-equiv="Content-Type">

I have already tried this and it doesn't work for me.

我已经尝试过这个,但它对我不起作用。

Here is my Node.js server code that serves the HTML-

这是我的 Node.js 服务器代码,用于提供 HTML-

var http = require('http');
var url = require('url');
var fs = require('fs');

var server = http.createServer(function(request, response) {
    console.log('connected');
    var path = url.parse(request.url).pathname;

    switch(path) {
        case "/realtime-graph-meter.html":
            fs.readFile(__dirname + path, function(error, data) {
                if(error) {
                    response.writeHead(404);
                    response.write("404 not found");
                } else {
                    console.log("before");
                    response.writeHead(200, {"Content-Type": "text/html"});
                    response.write(data, "utf-8");
                    console.log("after");
                }
            });
            break;
        default:
            response.writeHead(404);
            response.write("404 not found");
            break;
    }
    response.end();
});

server.listen(3000);
.
.
.

The beforeand afterlog messages get printed correctly. I have verified that my HTML file is utf-8 encoded.

beforeafter日志消息得到正确打印。我已经验证我的 HTML 文件是 utf-8 编码的。

Any suggestions ?

有什么建议 ?

回答by Mogens TrasherDK

Just to put an end to this question.

只是为了结束这个问题。

The magic spell, to get rid of the warning "The character encoding of the HTML document was not declared.", you need to declare your charset in the header section of the HTML file.

魔法咒语,要摆脱警告“未声明 HTML 文档的字符编码。”,您需要在 HTML 文件的标题部分声明您的字符集。

<meta charset="UTF-8">

Also, it should be one of, if not the first, line in the section.

此外,它应该是该部分中的一行(如果不是第一行)。

回答by Hyman A.

Try:

尝试:

response.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});

If you note, the metatag you posted includes the charsetattribute. This is used by the browser to identify the character encoding. The metatag is an after-the-fact attempt to emulate what normally should be in the Content-Typeheader of the HTTP response. Modifying your code as shown above will include the information in the HTTP response header, which is the preferred way to do it.

如果您注意到,meta您发布的标签包含该charset属性。浏览器使用它来识别字符编码。该meta标记是事后尝试模拟Content-TypeHTTP 响应标头中通常应该包含的内容。如上所示修改您的代码将在 HTTP 响应标头中包含信息,这是执行此操作的首选方式。