简体   繁体   English

如何在javascript中编码UTF-8字符?

[英]How to encode UTF-8 character in javascript?

I have a problem with showing a value from mysql database. 我在显示mysql数据库中的值时遇到问题。 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. 所以值被保存为UTF-8在mysql数据库中(正确),我正在将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? 接下来我该怎么办?

The character encoding has to be set on the real HTTP response Content-Type header, not alone on the meta tag. 字符编码必须在真实的​​HTTP响应Content-Type标头上设置,而不能在meta标签上单独设置。 The meta tag is ignored when the HTML output is retrieved by a HTTP request. 通过HTTP请求检索HTML输出时,将忽略meta标记。 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: 在webbrowser的开发人员工具集中,您可以通过在Chrome / IE9 / Firebug中按F12来获得,您必须能够浏览HTTP响应标头,如下所示:

在此处输入图片说明

Based on the comments you're apparently using PHP to produce HTML output to the HTTP response. 基于这些注释,您显然正在使用PHP将HTML输出生成为HTTP响应。 You should then be using its header() function to set the proper response header. 然后,您应该使用其header()函数设置正确的响应头。 Add the following line to your PHP script before any character is been written to the response. 在将任何字符写入响应之前 ,将以下行添加到PHP脚本中。

header("Content-Type: text/html;charset=UTF-8");

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM