Jquery ignores encoding ISO-8859-1

回眸只為那壹抹淺笑 提交于 2019-12-17 06:53:10

问题


I have a website which apperently removes the correct encoding (ISO-8859-1) from a string and sends it wrong.

I have this encoding specified in my HTML

<meta charset="ISO-8859-1">

I load my javascript via

<script type="text/javascript" charset="ISO-8859-1" src="...

I send for Information via JQuery Ajax Request like this (with german special character 'ö' and 'ä'):

$.ajax({
    url: '..',
    type: 'POST',
    contentType: 'application/xml;charset=ISO-8859-1',
    data: xmlRequest.html(),...

This is translated into a request and in the chrome developer tools I see this in the Request Header:

..
Content-Type: application/xml;charset=UTF-8
..

What happened there?

Of course the special characters are encoded wrong ("ö" instead of "ö") the server can't understand me and i get an error.


回答1:


Because I had the same problem, I'll provide a solution that worked for me. Background: Microsoft Excel is too stupid to export a CSV-File in charset UTF-8:

$.ajax({
    url: '...',
    contentType: 'Content-type: text/plain; charset=iso-8859-1',
    // This is the imporant part!!!
    beforeSend: function(jqXHR) {
        jqXHR.overrideMimeType('text/html;charset=iso-8859-1');
    }
});



回答2:


According to the jQuery.ajax() contentType documentation:

Data will always be transmitted to the server using UTF-8 charset; you must decode this appropriately on the server side."



来源:https://stackoverflow.com/questions/8988573/jquery-ignores-encoding-iso-8859-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!