本问题已经有最佳答案,请猛点这里访问。
我有一个编码为ISO-8959-9的页面。我将Ajax请求发送到同一个页面,同时将一些数据保存到数据库。但它将字符转换为UTF-8。我的响应头似乎与字符集ISO-8859-9很好。但是请求头、内容类型的数据总是UTF-8。请参阅下面的屏幕截图。下面是我为解决这个问题所做的:1-i设置php头iso-8859-92-我将Apache的默认字符集更改为ISO。3-我将ajax选项beforesend、setrequestheader和contenttype设置为iso。4-我修改了jquery.js并将ajax默认编码设置为iso。他们都没有解决我的问题。我不想做任何PHP字符集编码btw。还有其他想法吗?谢谢
我的AJAX代码:`
$.ajax({
url: window.location.href,
type: 'POST',
data: $(this).serialize(),
contentType:"application/x-www-form-urlencoded; charset=iso-8859-9",
success: function(result) {
$('#IcerikContent').html($(result).find("#Icerik"));
$('html, body').animate({scrollTop: 0}, 500);
Metronic.initAjax();
if (typeof initialize == 'function') { initialize(); }
stopPageLoading();
}
});
`
恐怕Ajax Post请求必须使用UTF-8。jquery手册解释了这一点:
POST data will always be transmitted to the server using UTF-8 charset, per the W3C XMLHTTPRequest standard.
现在您可能会想知道contentType设置:
Note: The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding.
换句话说,你别无选择。您要么需要将服务器端代码迁移到UTF-8,进行显式转换—iconv()和mb_convert_encoding()将派上用场—要么想出一个巧妙的javascript技巧(例如在提交前对数据进行序列化)。
嗯,你说得对。谢谢你的回答!我必须使用一些服务器端编码来克服这个问题。