JQuery: ajax post and coding

I cannot understand why I cannot get the correct charstet ISO-8859-1 from the server response. Since this is work on legacy code, I can hardly change the encoding of the encoding on the pages.

I am using 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> 

Server-side processing of the script request request in itself:

 text/html; charset=ISO-8859-1 

so to be honest, I can't figure out what else I have to declare in terms of coding. Despite this, accented characters "aééìòù" are returned as "ÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃ" when the server response is placed in the HTML element

The source is saved as ascii. Try this to have rudimentary Html coding in the variable you want to publish 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?

Thank!

+9
jquery
Jun 08 '09 at 14:24
source share
9 answers

I now have the best solution. Both posts and get the job EXCELLENT. I am working on tomcat, which by default processes ISO 8859 data.

Webpage Properties:

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

webpage encoding inside the head.

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

Now all my parameters are escaped using the escape function, I provided this solution earlier.

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

Now, by sending an ajax request, I set the contentType for this:

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

And finally, when getting parameters in a 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 work fine in IE 7 and 8, SAFARI, CHROME and FIREFOX.

+5
Aug 04 '09 at 17:58
source share

I used the following in javascript

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

and in php

 urldecode($var) 

It worked for me.

+2
Sep 13 '12 at 15:41
source share

I'm not sure if this is a violation of your situation, but accept-charset extremely poorly supported, and you may not even use it. This is the encoding of the page that will control what is sent back to the server.

It would be useful if you looked at the stored files on your server to find out if the data in them is good or not. This will at least establish whether the client-> back end of the transaction is working.

+1
Jun 08 '09 at 14:42
source share

Try using escape () in javascript and urldecode () in php file

he will work.

+1
Nov 11 '10 at 9:06
source share

As far as I know, jQuery uses a webpage encoding that calls the AJAX method to interpret the received data. Are you using the correct encoding for the page? If so, then most likely the error is related to the server code. Try programming the AJAX handler (outside the web browser) to see what it returns.

0
Jun 08 '09 at 14:36
source share

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

try converting utf-8 string with utf8_decode () to php with incoming and outgoing data.

0
Jul 02 '09 at 13:16
source share

All my pages are ISO-8859-1, my answer is also 8859, but jquery ajax beat me. My solution was to wrap the original $ .ajax functionality. Works perfect for a GET request, and I will post a solution for a POST request soon.

Call:

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

output function source

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

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); 
0
Aug 04 '09 at 13:47
source share

I was looking high and low for a similar problem (request Windows-1250 with AJAX). I found a solution and posted it here: How can encodeURIComponent encoded variables be encoded with ISO-8859-1 rather than utf-8 encoding?

0
Mar 24 2018-11-11T00:
source share

You may have forgotten to declare the contentType on the server side before specifying it as UTF-8, in which case the contentType is null and the server does not know what data it is dealing with. When you declare contentType as UTF-8, the server is able to understand what kind of data is working. Declaring contentType as UTF-8 is not the right solution, although it does help. The answer provided by Rodrigo would probably be the best solution for your problem.

0
Jul 25 '13 at 5:12
source share



All Articles