Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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');
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...