Ajax,向ISO Charset请求标头UTF-8 [英] Ajax, Request Header UTF-8 to ISO Charset

查看:305
本文介绍了Ajax,向ISO Charset请求标头UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个编码为iso-8959-9的页面.我将ajax请求发送到同一页面,同时将一些数据保存到数据库.但是它将字符转换为utf-8. 我的响应头对于charset iso-8859-9似乎不错.但是,请求标头,内容类型数据始终为UTF-8.请参考下面的截图. 香港专业教育学院为解决这个问题而做的事情:
1-我设定了PHP标头iso-8859-9
2-我将apache的默认字符集更改为iso.
3-我将ajax选项的beforeSend,setRequestHeader和contentType设置为iso.
4-我修改了jquery.js并将ajax默认编码设置为iso.
他们都没有解决我的问题.我不想对btw进行任何PHP字符集编码.
还有其他想法吗?

谢谢

我的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数据将始终按照W3C XMLHTTPRequest标准使用UTF-8字符集传输到服务器.

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

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

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


I have a page which is encoded iso-8959-9. Im sending ajax requests to same page while saving some data to DB. But its converts the chars to utf-8. My response header seems good with charset iso-8859-9. But the Request Header, Content-Type data always UTF-8. please refer to screenshot below. Here is what ive done to solve this:
1- I set php header iso-8859-9
2- I changed apache's default charset to iso.
3- i set ajax options beforeSend, setRequestHeader and contentType as iso.
4- i modified jquery.js and set ajax default encoding as iso.
none of them didnt solve my problem. i dont want to do any php charset encoding btw.
Any other ideas ?

Thanks

my ajax code: `

                $.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();
                    }
                });
        `

解决方案

I'm afraid that AJAX POST requests must use UTF-8. The jQuery manual explains it:

POST data will always be transmitted to the server using UTF-8 charset, per the W3C XMLHTTPRequest standard.

You might now wonder about the contentType setting:

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.

In other words, you have no choice. You either need to migrate your server-side code to UTF-8, make an explicit conversion —iconv() and mb_convert_encoding() will come in handy— or figure out a witty JavaScript trick (such as serialising data before submission).

这篇关于Ajax,向ISO Charset请求标头UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆