Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery ignores encoding ISO-8859-1

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.

like image 903
Spike2050 Avatar asked Jan 24 '12 14:01

Spike2050


Video Answer


2 Answers

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');
    }
});
like image 129
iappwebdev Avatar answered Oct 05 '22 13:10

iappwebdev


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

like image 35
JMM Avatar answered Oct 05 '22 11:10

JMM