Chrome ajax extension to send invalid characters with an accent

I am sending an AJAX POST request using jQuery to the chrome extension, but the data does not arrive as expected, the shock characters are incorrect.

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

The text is displayed in the console, etc., only through AJAX to this other page, as described above. My AJAX call is basic, I am sending a data object via jQuery $ .ajax. I tried using without contentType, UTF-8 and ISO-8859-1. No difference.

Here's 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 am extracting them from the form. However, I tried to specify these values ​​manually as newValues['name'] = 'ÄÄÄÄ'; and still cause the same problem.

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

The target site uses Servlet/2.5 JSP/2.1 . Just conclude that this may affect.

I assume this is a problem with the encoding, and as I understand it, it should be because the Chrome extensions require the script files to be encoded in UTF-8 encoding, which probably conflicts with the site the plugin is running on and the target an AJAX page (same site) that uses ISO-8859-1 encoding, however I have no idea how to deal with this. I tried several methods to decode / encode it, and from UTF-8 to ISO-8859-1 and other tricks without success.

I tried using encodeURIComponent for my values, which only makes them that way exactly so that the form displaying the values ​​sent via POST, for example. H%C3%84GERSTEN .

I do not have access to the website server and you cannot say if this is a problem on their part, but I would not do that.

UPDATE

Now I realized that the POST data should be sent as UTF-8! So conversion is not a problem?

+7
javascript jquery google-chrome ajax google-chrome-extension
source share
4 answers

I do not know if this was solved using POST data and AJAX. Perhaps if I made a pure XHR AJAX JavaScript request, I could send the POST data encoded as I like. I have no idea.

However, in my desperation, I tried my last option (or whatever seemed to him); send the request as GET data. I got lucky and the landing page accepted the GET data.

Obviously, the problem still persisted as I was sending data the same way, being UTF-8 encoded. So instead of sending the data as an object, I parsed the data in a friendly URL string with my own function, using escape , making sure they are compatible with ISO-8859-1 (since encodeURIComponent encodes the URI as UTF-8, and escape encodes the strings, which makes compatible with ISO-8859-1).

A simple function that cured my headaches:

 function URLEncodeISO(values) { var params = []; for(var k in values) params[params.length] = escape(k) + '=' + escape(values[k]); return params.join('&'); } 
+2
source share

It appears that the data is encoded in UTF-8 when it is sent and incorrectly decoded on the server side. It must be decoded on the server side. Test it with the following encoding and decoding 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> 
+1
source share

Character encoding on the client side is not entirely up to you (implement the use of the page from different users around the world: Chinese, Italian ...), but on the server side you need to process the encoding for your purposes.

Thus, the data in Ajax-POST can still be encoded in UTF8, but on the side of your server you do the following:

PHP:

$ name = utf8_decode ($ _ POST ['name']);

JSP:

  request.setCharacterEncoding ("UTF-8");
 String name = request.getParameter ("name");
+1
source share

We also faced the same situation, but in our case, we always sent parameters using JSON.stringify .
To do this, you need to make changes,
1) When calling the page via AJAX, you need to add a content-type tag, in which encoding data is transmitted

 $.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 found out that on the JSP server side I use ISO-8859-1 and read some messages, I found out that all POST method data will be transmitted using UTF-8 as indicated.

POST data will always be transmitted to the server using UTF-8 encoding according to the W3C XMLHTTPRequest standard

But when reading the jquery-ignores-encoding-iso-8859-1 iappwebdev , the iappwebdev workaround iappwebdev , which may 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'); } }); 

The above code is taken from Code Submitted by iappwebdev

+1
source share

All Articles