[英]How to handle UTF-8 in javascript strings
I have just upgraded from mysql 5.5 to 5.6 and the default encoding in version 5.6 is UTF-8. 我刚刚从mysql 5.5升级到5.6版本,5.6版本的默认编码是UTF-8。
All ajax requests are handled in UTF-8 too, so I decided to convert data in the database and the encoding on the website to UTF-8. 所有的ajax请求也以UTF-8处理,所以我决定将数据库中的数据和网站上的编码转换为UTF-8。
But now I'm experiencing a problem.. All special (unicode) chars on the website are displayed incorrect.. They are displayed as encoded UTF-8 strings (double bytes) 但现在我遇到了一个问题..网站上的所有特殊(unicode)字符显示不正确..它们显示为编码的UTF-8字符串(双字节)
The whole site is build in 100% jquery, and all strings are provided by ajax requests and appended/written with jquery... I can't figure out what I'm doing wrong?! 整个站点是100%jquery构建的,所有字符串都是由ajax请求提供的,并附加/写入jquery ...我无法弄清楚我做错了什么?!
$dbh = new PDO($driver.':dbname='.$db.';host='.$host.';port='.$port.';charset=utf8', $user, $pass);
All tables and columns in the database is converted to UTF-8_bin 数据库中的所有表和列都将转换为UTF-8_bin
header('Content-Type: text/html; charset=utf-8');
<meta charset="utf-8" />
The HTML page is encoded in UTF-8. HTML页面以UTF-8编码。 Is both sending a header from PHP and have added a meta tag in the head in the HTML document
是从PHP发送标头还是在HTML文档的头部添加了元标记
header('Content-Type: application/json; charset=utf-8');
{
type : 'post',
async : true,
cache : false,
dataType : 'json',
timeout : 15000,
contentType : 'application/x-www-form-urlencoded;charset=utf-8',
global : true,
url : APIURL,
data : {},
success : function(){}
}
All Ajax requests is made with these properties 所有Ajax请求都是使用这些属性创建的
{"result":[{"id":"391","string":"BTN_ADD_ACCOUNTS","da":"Tilf\u00c3\u00b8j regnskab","en":"Add accounts"},{"id":"321","string":"BTN_ADD_ENTRY" ...
The value Tilf\Ã\¸j regnskab
should be displayed as Tilføj regnskab
on the page, but it is displayed as Tilføj regnskab
值
Tilf\Ã\¸j regnskab
应在页面上显示为Tilføj regnskab
,但它显示为Tilføj regnskab
When browsing the database in phpmyadmin all data showed corretcly 在phpmyadmin中浏览数据库时,所有数据都显示正确
I can put data in the database via Ajax requests and the data is stored correctly, but can't retrieve data via Ajax requests 我可以通过Ajax请求将数据放入数据库中,并且数据存储正确,但无法通过Ajax请求检索数据
header('Content-Type: text/html; charset=utf-8');
$connect = $driver.':dbname='.$db.';host='.$host.';port='.$port.';charset=utf8';
$dbh = new PDO($connect, $user, $pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $dbh->prepare("SELECT da FROM lang WHERE string='HDL_CLIENT'");
$sth->execute();
$row = $sth->fetch(PDO::FETCH_ASSOC);
print_r($row);
// result
klient æå
I hadn't noticed the important bit. 我没有注意到重要的一点。 As icktoofay points out, your JSON is already corrupted:
正如icktoofay指出的那样,您的JSON已经损坏:
>>> "Tilf\u00c3\u00b8j regnskab"
"Tilføj regnskab"
You have two characters ( \Ã\¸
) to represent ø
(which is obviously one character). 你有两个字符(
\Ã\¸
)来表示ø
(显然是一个字符)。 Since ø is U+00F8 it should be just \ø
: 由于ø是U + 00F8,它应该只是
\ø
:
>>> "Tilf\u00f8j regnskab"
"Tilføj regnskab"
The problem is in the PHP script that generates that JSON, not in the JavaScript code that consumes it. 问题出在生成JSON的PHP脚本中,而不是在使用它的JavaScript代码中。 Possibilities:
可能性:
The UTF-8 representation for ø is "0xC3 0xB8 (c3b8)". ø的UTF-8表示为“0xC3 0xB8(c3b8)”。 To need to ensure that you see those bytes.
需要确保您看到这些字节。 To print hexadecimal values:
要打印十六进制值:
SELECT HEX(column_name) FROM table_name
SELECT HEX(column_name) FROM table_name
var_dump( bin2hex($variable) );
var_dump( bin2hex($variable) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.