javascript 如何在javascript中编码UTF-8字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11228810/
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
How to encode UTF-8 character in javascript?
提问by user123_456
I have a problem with showing a value from mysql database. So value is saved as UTF-8 in the mysql database ( correctly ) , I am retrieving a JSON formated data to javascript (correctly) and then when I print the result in the javascript I don't see right signs as I am using Croatian alphabet.
我在显示 mysql 数据库中的值时遇到问题。因此,值在 mysql 数据库中保存为 UTF-8(正确),我正在将 JSON 格式的数据检索到 javascript(正确),然后当我在 javascript 中打印结果时,我没有看到正确的标志,因为我使用的是克罗地亚语字母。
I have put this in the head section:
我已经把它放在头部部分:
<meta name="http-equiv" content="Content-type: text/html; charset=UTF-8"/>
and in the script section:
<meta name="http-equiv" content="Content-type: text/html; charset=UTF-8"/>
并在脚本部分:
<script type="text/javascript" charset="utf-8">
What can I do next?
我接下来可以做什么?
回答by BalusC
The character encoding has to be set on the real HTTP response Content-Type
header, not alone on the meta tag. The meta tag is ignored when the HTML output is retrieved by a HTTP request. In webbrowser's developer toolset as you can get by pressing F12 in Chrome/IE9/Firebug, you must be able to explore the HTTP response headers like below:
字符编码必须在真正的 HTTP 响应Content-Type
头上设置,而不仅仅是在元标记上。当 HTTP 请求检索 HTML 输出时,元标记将被忽略。在浏览器的开发者工具集中,你可以通过在 Chrome/IE9/Firebug 中按 F12 获得,你必须能够探索如下的 HTTP 响应头:
Based on the comments you're apparently using PHP to produce HTML output to the HTTP response. You should then be using its header()
function to set the proper response header. Add the following line to your PHP script beforeany character is been written to the response.
根据评论,您显然是在使用 PHP 为 HTTP 响应生成 HTML 输出。然后你应该使用它的header()
函数来设置正确的响应头。在将任何字符写入响应之前,将以下行添加到您的 PHP 脚本中。
header("Content-Type: text/html;charset=UTF-8");