admin管理员组文章数量:1023221
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?
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?
Share Improve this question asked Jun 27, 2012 at 14:42 user123_456user123_456 5,82526 gold badges87 silver badges142 bronze badges 5-
1
What server side view technology are you using? It needs to be set as
charset
attribute of the HTTP responseContent-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 afile://
URI instead of ahttp://
URI. – BalusC 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 – user123_456 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? – BalusC 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 – user123_456 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. – BalusC Commented Jun 27, 2012 at 15:15
1 Answer
Reset to default 1The 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 ments 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:
- PHP UTF-8 cheatsheet
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?
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?
Share Improve this question asked Jun 27, 2012 at 14:42 user123_456user123_456 5,82526 gold badges87 silver badges142 bronze badges 5-
1
What server side view technology are you using? It needs to be set as
charset
attribute of the HTTP responseContent-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 afile://
URI instead of ahttp://
URI. – BalusC 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 – user123_456 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? – BalusC 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 – user123_456 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. – BalusC Commented Jun 27, 2012 at 15:15
1 Answer
Reset to default 1The 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 ments 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:
- PHP UTF-8 cheatsheet
本文标签: utf 8How to encode UTF8 character in javascriptStack Overflow
版权声明:本文标题:utf 8 - How to encode UTF-8 character in javascript? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745590542a2157859.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论