简体   繁体   中英

How to encode UTF-8 character in javascript?

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.

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:

<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. 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:

在此处输入图片说明

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 before any character is been written to the response.

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

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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