tags:

views:

4801

answers:

5

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!

A: 

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
I am using: <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
Daniel
+1  A: 

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
A: 

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.

A: 

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
+1  A: 

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"&gt;

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