使用Ajax时UTF8编码不工作 [英] UTF8 encoding not working when using ajax
问题描述
最近,我改变了我的一些网页,通过AJAX进行显示和我有一些困惑,为什么utf8编码,现在显示问号的箱子内,而以前不是这样的。
I recently changed some of my pages to be displayed via ajax and I am having some confusion as to why the utf8 encoding is now displaying a question mark inside of a box, whereas before it wasn't.
富勒例子。该oringal网页的index.php。字符集显式设置为utf8,并在< HEAD>
。然后我用PHP来查询数据库
Fore example. The oringal page was index.php. charset was explicitly set to utf8 and is in the <head>
. I then used php to query the database
继承人是原来的index.php页面:
Heres is the original index.php page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Title here</title>
</head>
<body class='body_bgcolor' >
<div id="main_container">
<?php
Data displayed via php was simply a select statement that output the HTML.
?>
</div>
然而,当我进行了更改,以添加填充所有的utf8编码停止工作,通过AJAXmain_container的菜单。这里是新的code:
However, when I made the change to add a menu that populated the "main_container" via ajax all the utf8 encoding stopped working. Here's the new code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Title here</title>
</head>
<body class='body_bgcolor' >
<a href="#" onclick="display_html('about_us');"> About Us </a>
<div id="main_container"></div>
在display_html()函数调用JavaScript的网页,其中使用jQuery的AJAX调用来检索存储在PHP页面中HTML,然后用main_container的ID则以DIV中的HTML。我设置的字符集jQuery中是UTF8喜欢的:
The "display_html()" function calls the javascript page which uses jquery ajax call to retrieve the html stored inside a php page, then places the html inside the div with an id of "main_container". I'm setting the charset in jquery to be utf8 like:
$.ajax({
async: false,
type: "GET",
url: url,
contentType: "charset=utf-8",
success: function(data)
{
$("#main_container").html(data);
}
});
我是什么做错了吗?
What am I doing wrong?
推荐答案
编码超过指定meta标签和内容类型 - 文件本身必须真的在你指定的编码,或者你会得到的mojibake 。
Encoding is more than specifying the meta tag and content type - the files themselves must really be in the encoding you specify, or you'll get mojibake.
检查,一切都使用UTF-8,你的数据库,数据库连接,表列。检查任何静态文件,你也包括连接codeD的UTF-8。
Check that everything is using UTF-8, your database, database connection, table columns. Check that any static files you are including are also encoded in UTF-8.
这篇关于使用Ajax时UTF8编码不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!