首页 文章

Ajax,请求标头UTF-8到ISO字符集[重复]

提问于
浏览
1

这个问题在这里已有答案:

我有一个编码iso-8959-9的页面 . 我将ajax请求发送到同一页面,同时将一些数据保存到DB . 但它将角色转换为utf-8 . 我的响应 Headers 似乎很好用charset iso-8859-9 . 但是Request Header,Content-Type数据总是UTF-8 . 请参考下面的截图 . 以下是我为解决这个问题所做的工作:
1-我设置php标头iso-8859-9
2-我将apache的默认字符集更改为iso .
3-我设置ajax选项beforeSend,setRequestHeader和contentType为iso .
4-我修改了jquery.js并将ajax默认编码设置为iso .
他们都没有解决我的问题 . 我不想做任何php charset编码顺便说一句 .
还有其他想法吗?

谢谢

enter image description here

我的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();
                    }
                });
        `

1 回答

  • 1

    我担心AJAX POST请求 must 使用UTF-8 . jQuery manual explains it

    根据W3C XMLHTTPRequest标准,POST数据将始终使用UTF-8字符集传输到服务器 .

    您现在可能想知道 contentType 设置:

    注意:W3C XMLHttpRequest规范规定字符集始终为UTF-8;指定另一个字符集不会强制浏览器更改编码 .

    换句话说,你别无选择 . 您需要将服务器端代码迁移到UTF-8,进行显式转换 - iconv()mb_convert_encoding() 会派上用场 - 或者找出一个机智的JavaScript技巧(例如在提交前序列化数据) .

相关问题