Search This Blog

Sunday, December 6, 2009

Jquery: ajax post and encoding

Works fine in my side.. Thanks to Rodrigo Asensio.

From http://stackoverflow.com/questions/965150/jquery-ajax-post-and-encoding/1229012#1229012

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.

1 comment:

  1. I found this post with a google alert with my name. Pretty awesome! Thanks for say thanks.

    I have also another solution to work 100% with UTF8 ( the default of Jquery ) and not the default in tomcat.

    Have a good one.
    Rodrigo Asensio

    ReplyDelete