jQuery AJAX 字符编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/553463/
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
jQuery AJAX Character Encoding
提问by Salty
I'm currently coding a French website. There's a schedule page, where a link on the side can be used to load another day's schedule.
我目前正在编写一个法语网站。有一个时间表页面,侧面的链接可用于加载另一天的时间表。
Here's the JS I'm using to do this:
这是我用来执行此操作的 JS:
<script type="text/javascript">
function load(y) {
$.get(y,function(d) {
$("#replace").html(d);
mod();
});
}
function mod() {
$("#dates a").click(function() {
y = $(this).attr("href");
load(y);
return false;
});
}
mod();
</script>
The actual AJAX works like a charm. My problem lies with the response to the request.
实际的 AJAX 就像一个魅力。我的问题在于对请求的响应。
Because it is a French website, there are many accented letters. I'm using the ISO-8859-15 charset for that very reason. However, in the response to my AJAX request, the accents are becoming ?'s because the character encoding seems to be changed back to UTF-8.
因为是法语网站,所以有很多带重音的字母。正是出于这个原因,我正在使用 ISO-8859-15 字符集。但是,在对我的 AJAX 请求的响应中,重音变成了 ?'s,因为字符编码似乎改回 UTF-8。
How do I avoid this? I've already tried adding some PHP at the top of the requested documents to set the character set:
我如何避免这种情况?我已经尝试在请求的文档顶部添加一些 PHP 来设置字符集:
<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?>
But that doesn't seem to work either. Any thoughts?
但这似乎也不起作用。有什么想法吗?
Also, while any of you are looking here...why does the rightmost column seem to become smaller when a new page is loaded, causing the table to distort and each <li>
within the <td>
to wrap to the next line?
另外,虽然你们中的任何人都在看这里...为什么在加载新页面时最右边的列似乎变小了,导致表格扭曲并且表格中的每个<li>
都<td>
换行到下一行?
Cheers
干杯
采纳答案by yoavf
UTF-8 is supposed to handle all accents and foreign chars - why not use it on your data source?
UTF-8 应该处理所有的重音和外来字符——为什么不在你的数据源上使用它呢?
EDIT
[Archive copy of the test file.] with your data
编辑
[测试文件的存档副本。] 与您的数据
Everything should be UTF-8 in the first place. I loaded the files in notepad++, converted to utf-8 and manually changed the charactes to accents were needed. Once done everything's working like a charm.
一切都应该首先是 UTF-8。我在记事本++中加载文件,转换为utf-8并手动将字符更改为需要的重音符号。一旦完成,一切都会像魅力一样工作。
BTW, unless your server is defined to php-process .html files, the files you're loading with ajax aren't getting your iso charset. If you insist on using the iso charset, request a php file instead of an html file, and define the charset in the header (not in the file itself)
顺便说一句,除非您的服务器定义为 php-process .html 文件,否则您使用 ajax 加载的文件不会获得您的 iso 字符集。如果你坚持使用iso字符集,请求一个php文件而不是一个html文件,并在header中定义字符集(而不是文件本身)
回答by Magnar
Specifying the content type on the AJAX-call solved my problems on a Norwegian site.
在 AJAX 调用上指定内容类型解决了我在挪威站点上的问题。
$.ajax({
data: parameters,
type: "POST",
url: ajax_url,
timeout: 20000,
contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
dataType: 'json',
success: callback
});
You would also have to specify the charset on the server.
您还必须在服务器上指定字符集。
<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?>
回答by anony
$str=iconv("windows-1250","UTF-8",$str);
what helped me on the eventually
是什么最终帮助了我
回答by kgiannakakis
You need to set up your server to use ISO-8859-15 as the character encoding (adding the appropriate HTTP header). Doing it in the body of the html won't help.
您需要将服务器设置为使用 ISO-8859-15 作为字符编码(添加适当的 HTTP 标头)。在 html 的正文中执行此操作无济于事。
I can see this line
我可以看到这条线
<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?>
at the source of your html. This shouldn't happen. Using Live HTTP Headers I can't see the appropriate charset HTTP header. Use that for both your first page and the ajax service.
在你的 html 的来源。这不应该发生。使用实时 HTTP 标头我看不到相应的字符集 HTTP 标头。将它用于您的第一页和 ajax 服务。
回答by Doug
I would strongl suggest the use of the javascript escape() method
我强烈建议使用 javascript escape() 方法
you can use this with jQuery by grabbing a form value like so:
您可以通过抓取表单值来使用 jQuery,如下所示:
var encodedString = escape($("#myFormFieldID").val());
回答by Rafael - orafaelreis
For my PHP application the solution was include utf8_decode function for each variable get in $_POST
.
对于我的 PHP 应用程序,解决方案是为每个变量 get in 包含 utf8_decode 函数$_POST
。
Ex: (SERVER SIDE)
例如:(服务器端)
if( strtoupper($_SERVER['REQUEST_METHOD']) == "POST" ){
$variable = htmlentities($_POST['variable']); // wrong
$variable = htmlentities( utf8_decode($_POST['variable']) ); // right
}
Abra?os!!! (Bye Bye!!!)
阿布拉?奥斯!!!(再见!!!)
回答by ólafur Waage
When printing out the variables in the ajax file. Put a
打印出ajax文件中的变量时。放一个
htmlentities()
Around them, see if it works. Worked for me in an Icelandic ajax application.
在他们周围,看看它是否有效。在冰岛语 ajax 应用程序中为我工作。
回答by Sebastian Alberoni
I was having similar problems, working in a content comments system in our Spanish Portal.
What finally solved my problem, after many hours of searching, instead of messing with jQuery charset, that seems to use utf-8 no matter what, it was to decode from utf-8 back to ISO-8859-1 in the PHP that processed the ajax POST. In PHP there is a built in function, utf8_decode()
, so the first thing I do with the comments string is this:
我遇到了类似的问题,在我们的西班牙门户网站的内容评论系统中工作。经过数小时的搜索,最终解决了我的问题,而不是搞乱 jQuery 字符集,似乎无论如何都使用 utf-8,它是在处理的 PHP 中从 utf-8 解码回 ISO-8859-1 ajax POST。在 PHP 中有一个内置函数, utf8_decode()
所以我对注释字符串做的第一件事是:
$comentario = utf8_decode($_POST['comentario']);
(and then I used nl2br()
and htmlentities()
PHP functions in order to prepare the text to be stored with html entities instead of special chars)
(然后我使用nl2br()
和htmlentities()
PHP 函数来准备要与 html 实体一起存储的文本而不是特殊字符)
Good Luck & Peace all over! Seba
祝你好运与和平!塞巴
回答by Kyuss
I have been fiddling around with this problem and found out that this solution works for Firefox and safari (yes, im on a mac at the moment).
我一直在摆弄这个问题,发现这个解决方案适用于 Firefox 和 safari(是的,我目前在 mac 上)。
when getting the request, i made a content-type=iso-8859-1 here:
收到请求时,我在这里做了一个 content-type=iso-8859-1 :
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType) {
httpRequest.overrideMimeType('text/xml; charset=ISO-8859-1');
}
}
Please tell me if someone finds out this doesn't work in ie.
如果有人发现这在 ie 中不起作用,请告诉我。
回答by yngling
I had an issue with Swedish/Norwegian letters showing up as question marks, despite having specified:
我遇到了瑞典/挪威字母显示为问号的问题,尽管已指定:
contentType: "application/json; charset=utf-8",
This was solved by adding encodeURIComponent
to the string.
这是通过添加encodeURIComponent
到字符串来解决的。
url: "/set_comment.do?text=" + encodeURIComponent(comment),