2

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?

5
  • 1
    What server side view technology are you using? It needs to be set as charset attribute of the HTTP response Content-Type header over there. The meta tag is ignored on HTTP responses and is for example only used when you let your webbrowser save the HTML output to disk and then open it from disk by a file:// URI instead of a http:// URI. Commented Jun 27, 2012 at 14:51
  • How do you mean what server side view technology am I using? I am getting data in JSON format and it has utf-8 sign visible Commented Jun 27, 2012 at 15:03
  • Uh, simply put, what are you using to produce HTML? PHP, ASP, JSP, JSF, Python, RoR, etc..? Or is it a static HTML file and are you using a HTTP server without any default builtin support for a view technology like Apache HTTPD? Commented Jun 27, 2012 at 15:04
  • now you got me a bit confused. Page is written in static html, but I am using javascript as I have a google maps on it. So I am pulling data from the server in order to fill the data in google maps. I have just tried to write something into console.log() from javascript in croatian alphabet and it is working correctly so problem might be in when retreiving data from server. Now I see that JSON is not giving me the right value. So it is php problem Commented Jun 27, 2012 at 15:11
  • So, you're using PHP as view technology? Mention (and tag) that as such then. I've posted an answer. Commented Jun 27, 2012 at 15:15

1 Answer 1

1

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:

enter image description here

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:

Sign up to request clarification or add additional context in comments.

3 Comments

I have put this: header("Content-type: application/json; charset=UTF-8"); and still I have the same problem
Then you've also a problem in the way how you're retrieving and printing it. See the PHP UTF-8 cheatsheet for that. Make sure that the data is retrieved from MySQL using UTF-8 and make sure that PHP is printing it using UTF-8. If you're using old fashioned mysql_* functions instead of PDO, you also need to execute SET NAMES 'utf8' beforehand.
Well, if you're absolutely positive that you're using UTF-8 through all layers from the database to the model, the controller and the view, then it's apparently not correctly stored as UTF-8 in the database in the first place at all. Do note that this problem is absolutely not in JavaScript as it inherently supports Unicode BMP already. Concentrate more on the MySQL/PHP configuration and the HTTP response handling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.