Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension ajax sending malformed accented characters

I am sending an AJAX POST request using jQuery on a chrome extension but the data doesn't arrive as expected, accented characters turn out malformed.

The text "HÄGERSTEN" becomes "HÄGERSTEN".

The text appears fine in the console etc, only via AJAX to this other page it appears as mentioned. My AJAX call is basic, I send a data-object via jQuery $.ajax. I've tried both with and without contentType, UTF-8 and ISO-8859-1. No difference.

This is how I make my AJAX call:

var newValues = {name: 'HÄGERSTEN'}

$.ajax({
    url: POST_URL,
    type: 'POST',
    data: newValues,
    success: function() ...
});

The newValues object has more values but I retrieve them from a form. However, I have tried to specify these values manually as newValues['name'] = 'ÄÄÄÄ'; and still would cause the same problem.

The original form element of the page that I am sending the AJAX to contains attribute accept-charset="iso-8859-1". Maybe this matters.

The target website is using Servlet/2.5 JSP/2.1. Just incase it might make a difference.

I assume this is an encoding issue and as I've understood it should be because Chrome extensions require the script files to be UTF-8 encoded which probably conflicts with the website the plugin is running on and the target AJAX page (same website) which is using an ISO-8859-1 encoding, however I have no idea how to deal with it. I have tried several methods of decoding/encoding it to and from UTF-8 to ISO-8859-1 and other tricks with no success.

I have tried using encodeURIComponent on my values which only makes them show that way exactly on the form that displays the values I have sent via POST, as e.g. H%C3%84GERSTEN.

I have no access to the websites server and cannot tell you whether this is a problem from their side, however I would not suppose so.

UPDATE

Now I have understood POST data must be sent as UTF-8! So a conversion is not the issue?

like image 438
Colandus Avatar asked Dec 16 '15 14:12

Colandus


2 Answers

Seems like the data is UTF-8 encoded when it is sent and not properly decoded on the server side. It has to be decoded on the server side. Test it out with the following encode and decode functions:

function encode_utf8(s) {
  return unescape(encodeURIComponent(s));
}

function decode_utf8(s) {
  return decodeURIComponent(escape(s));
}

var encoded_string = encode_utf8("HÄGERSTEN");
var decoded_string = decode_utf8(encoded_string);
document.getElementById("encoded").innerText = encoded_string;
document.getElementById("decoded").innerText = decoded_string;
<div>
Encoded string: <span id="encoded"></span>
</div>
<div>
Decoded string: <span id="decoded"></span>
</div>
like image 161
jianweichuah Avatar answered Sep 22 '22 00:09

jianweichuah


We too faced the same situation but in our case we always sent the parameters using JSON.stringify.
For this you have to make changes,
1) While making call to the page via AJAX you have to add content-type tag defining in which encoding data is sent

$.ajax
    ({
        type: "POST",
        url: POST_URL,
        dataType: 'json',//In our case the datatype is JSON
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(newValues),//I always use parameters to be sent in JSON format

EDIT
After reading your question more clearly I came to know that your server side JSP uses ISO-8859-1 encoding and reading some posts, I came to know that all POST method data will be transmitted using UTF-8 as mentioned.

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

But while reading post jquery-ignores-encoding-iso-8859-1 there was a workaround posted by iappwebdev which might be useful and help you,

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

Above code is taken from Code Posted by iappwebdev

like image 41
dbw Avatar answered Sep 19 '22 00:09

dbw