Ajax, Request Header UTF-8 to ISO Charset [duplica

2019-08-12 16:00发布

This question already has an answer here:


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

enter image description here

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

1条回答
我想做一个坏孩纸
2楼-- · 2019-08-12 16:41

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).

查看更多
登录 后发表回答