jQuery 使用ajax时UTF8编码不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2877852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UTF8 encoding not working when using ajax
提问by Ronedog
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.
我最近更改了我的一些页面以通过 ajax 显示,我对为什么 utf8 编码现在在框内显示问号有一些困惑,而在以前不是。
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。字符集被显式设置为 utf8 并且在<head>
. 然后我用php查询数据库
Heres is the original index.php page:
这是原始的 index.php 页面:
<!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>
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:
但是,当我进行更改以添加通过 ajax 填充“main_container”的菜单时,所有 utf8 编码都停止工作。这是新代码:
<!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>
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:
“display_html()”函数调用javascript页面,该页面使用jquery ajax调用来检索存储在php页面中的html,然后将html放置在id为“main_container”的div中。我将 jquery 中的字符集设置为 utf8,例如:
$.ajax({
async: false,
type: "GET",
url: url,
contentType: "charset=utf-8",
success: function(data)
{
$("#main_container").html(data);
}
});
What am I doing wrong?
我究竟做错了什么?
采纳答案by mdma
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.
编码不仅仅是指定元标记和内容类型 - 文件本身必须真正采用您指定的编码,否则您将获得mojibake。
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.
检查所有内容是否都使用 UTF-8、您的数据库、数据库连接、表列。检查您包含的任何静态文件是否也以 UTF-8 编码。
回答by Oleg
You wrote
你写了
The "display_html()" function calls the javascript page which uses jquery ajax call to retrieve the html stored inside a php page
"display_html()" 函数调用 javascript 页面,该页面使用 jquery ajax 调用来检索存储在 php 页面中的 html
What do you mean with "the html stored inside a php page"? If you want to load data and display there as a contain of <div>
the loaded data should be formated correspondent. I mean that it should be real a code fragment of HTML. Moreover Together with 'contentType' it would be a good idea to specify 'dataType' as "html" or "text". If you don't specity anything the last version of jQuery will "intelligently try to get the results, based on the MIME type of the response". If you know the 'dataType', it would be better to specify there. And if you use ajax use also default 'async: true' and not 'false'.
“存储在 php 页面中的 html”是什么意思?如果要加载数据并在那里显示为包含<div>
加载的数据,则应格式化相应的数据。我的意思是它应该是真正的 HTML 代码片段。此外,与“contentType”一起将“dataType”指定为“html”或“text”是个好主意。如果您不指定任何内容,jQuery 的最新版本将“根据响应的 MIME 类型智能地尝试获取结果”。如果您知道“dataType”,最好在那里指定。如果您使用 ajax,请使用默认的 'async: true' 而不是 'false'。
You should also verify whether jQuery.load
method (see http://api.jquery.com/load/) is the best choice for you. You can load with the mathod a full html page if required and display only a part of there: $('#main_container').load('ajax/about_us.html #container');
您还应该验证jQuery.load
方法(请参阅http://api.jquery.com/load/)是否是您的最佳选择。如果需要,您可以使用 mathod 加载完整的 html 页面并仅显示其中的一部分:$('#main_container').load('ajax/about_us.html #container');
And about UTF-8 encoding don't forget to save the file really UTF-8 encoded. Use corresponding option of your editor (in Notepad choose "Save As" and then choose as encoding "UTF-8" and not "ANSI").
关于 UTF-8 编码不要忘记保存真正 UTF-8 编码的文件。使用编辑器的相应选项(在记事本中选择“另存为”,然后选择“UTF-8”而不是“ANSI”作为编码)。
回答by Carlos M. Meyer
Make sure all your files are saved as UTF-8 (or UTF-8 w.o. BOM).
确保所有文件都保存为 UTF-8(或 UTF-8 wo BOM)。
If you have uploaded them by FTP or with a web tool, check if they are still UTF-8.
如果您已通过 FTP 或网络工具上传它们,请检查它们是否仍然是 UTF-8。
回答by Timur Gafforov
In my case neither of the solutions worked until I placed
在我的情况下,在我放置之前,两种解决方案都不起作用
header('Content-type: text/html; charset=utf-8');