9

I am unable to understand why I can't get a correct ISO-8859-1 charstet from the server answer. Being this a work on legacy code, i hardly could change charset encoding on the pages.

I make use of the JQuery call

$.post("server-side-code", {t:ctext, i:ioff, sid:sessionid},  
    function(data, status) {            
       $('#chk').append(data); 
     });

posting a textarea value created using javascript:

<form accept-charset='ISO-8859-1' method='post'>
<textarea cols='40' rows='8' id='commento'></textarea><br>
<input type='button' value='invia' id='submit'></form>

The server side script processing the request declares at its very top:

text/html; charset=ISO-8859-1

so, honestly, I can't figure out what else I should declare, in terms of encoding. This notwithstanding, the accented characters "àèéìòù" bounce back as: "à èéìòù" when placing the server answer in an HTML element

The source is saved as ascii. Tryng to do this to have rudimentary Html encoding on the variable to be posted does not solve:

ctext = escapeHTML(ctext);

function escapeHTML (str)
{
   var div = document.createElement('div');
   var text = document.createTextNode(str);
   div.appendChild(text);
   return div.innerHTML;
}; 

Some idea?

Thanks!

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
Daniel
  • 1,357
  • 2
  • 19
  • 39
  • Well... this is crazy. I cannot believe it. The problem is solved declaring UTF-8 the server side page returning answer to ajax request. I mean: declaring a charset which is DIFFERENT form the proper charset used in tha page making the ajax request. – Daniel Jun 08 '09 at 15:53

9 Answers9

5

I have a better solution now. Both post and get works PERFECTLY. I'm working over tomcat who by default handle ISO 8859 stuff.

Web page properties:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

charset of the webpage inside the head.

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

Now, all my parameteres are escaped with escape function, I provided this solution before.

(function($) {$.fn.escape = function() {
    return escape(this.val());};})(jQuery);

Now, when sending the ajax request I set the contentType to this:

contentType : "application/x-www-form-urlencoded; charset=iso-8859-1"

And finally , when receiving the parameters at the servlet or any receiver I get a decoded parameter using this.

    public String getHttpParameter(String name) throws Exception {
    String value = this.getHttpRequest().getParameter(name);
    return value == null || value.isEmpty() ? value : URLDecoder.decode(value,"ISO-8859-1");
}

POST and GET works perfectly in IE 7 and 8 , SAFARI, CHROME and FIREFOX.

Rodrigo Asensio
  • 2,760
  • 4
  • 26
  • 26
  • with this solutions I have problems when somebody sends the caracter '%'. URLDecoder.decode launch an exception (Ilegal argument - Incomplete trailing escape (%) pattern). – Oriol Terradas Feb 06 '12 at 11:05
2

I have used the following in javascript

escape($("#some_id").val()) 

and the following in PHP

urldecode($var)

It worked for me.

Tariqulazam
  • 4,535
  • 1
  • 34
  • 42
Ciro
  • 21
  • 2
1

Please try using escape() in javascript and urldecode() in php file

it will work.

1

Not sure if it's what's breaking your situation, but accept-charset is extremely poorly supported, and you may as well not even use it. It's the page encoding that will control what gets sent back to the server.

It'd be useful if you looked at the saved files on your server to see whether the data in them is good or not. That'd at least establish whether the client->server part of the transaction is working.

chaos
  • 122,029
  • 33
  • 303
  • 309
0

General information: http://www.w3.org/International/O-HTTP-charset

try to convert the utf-8 string with utf8_decode() in php with the incomming and outgoing data.

0

All my pages are ISO-8859-1, my response is also 8859 but jquery ajax was freaking me out. My solution was wrap the original $.ajax functionality with this. Works Perfectly for GET request, and I will post soon the solution for POST request.

The call:

$.myrequest({
  url: "/myapp/myurl",
  params: {
    key1: $("#myinput").escape()
  }
});

the source for escape function

(function($) {
$.fn.escape = function() {
    return escape(this.val());
};})(jQuery);

the source for myrequest function

(function($) {

$.request = function(settings) {

    /**
     * generates a string that will be passed as 
     * data instead an object, but we have the capability
     * of pass an object but in another variable called param
     */
    function _parseParameters(settings) {
        var data = "";
        for (var param in settings.params) {
            data += param + "=" + settings.params[param] + "&";
        }
        settings.data = data;
    }
    _parseParameters(settings);
    $.ajax(settings);
}})(jQuery);
Rodrigo Asensio
  • 2,760
  • 4
  • 26
  • 26
0

Maybe you have forgot to declare the contentType on the server side before to specify it as UTF-8, in this case, the contentType is null and the server don´t know what kind of data it´s dealing with. Once you declare the contentType as UTF-8, the server is capable to understand which kind of data is working with. Declaring the contentType as UTF-8 is not the correct solution, although it helps. The answer provided by Rodrigo probably would be the best solution for your problem.

Josue
  • 77
  • 7
0

I was searching high and low for a similar problem (Windows-1250 request with AJAX). I've found a solution and posted here: How can be request variables encoded by encodeURIComponent with ISO-8859-1 charset and not utf-8?

Community
  • 1
  • 1
Nux
  • 9,276
  • 5
  • 59
  • 72
0

As far as I know jQuery uses the encoding of the web page that invokes the AJAX method to interpret the received data. Are you using the correct encoding for the page? If yes, then most probably the error lies with the server code. Try calling the AJAX handler programmatically (outside the Web Browser) to see what it is returning.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194