4

I have both sending and receiving jsp pages are encoded in UTF-8 jquery.post call works except IE. Page inputs are expected to be in Turkish language. Turkish special characters çşığü messed up.

$.post('/answer.jsp', {cmd:'doSomething',nickname:nickNameUser}, 
 function(data){                
    if(data!=null && data.success){
        window.location.href="/main.htm";
    }
    else{
       $('#formError').text(data.error);    
    }                                       
 },
 'json');   

What I have tried but failed

1-) I convert receiving jsp file to ISO-8859-9 and convert post to ajax provided charset

$.ajax({type:'POST',
    url:'/answer.jsp',
    data:{cmd:'doSomething', nickname:NickNameUser},
    dataType:'json',
    contentType: 'application/json; charset=ISO-8859-9',
    cache: false,
    success:function(data){
        if(data!=null && data.success){
            window.location.href="/main.htm";
        }
        else{
            $('#formError').text(data.error);
        }                                       
    }
}); 

2-) I tried on Java side following

nickname = new String(nickname.getBytes("utf-8"), "iso-8859-9");

None of them worked. Do you know any workaround?. By the way I hate IE.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Gok Demir
  • 1,404
  • 4
  • 21
  • 40

2 Answers2

2

You can also use :

url=encodeURI(url);

and then put it as a param:

$.ajax({type:'POST',
    url:url,.....
sakis
  • 21
  • 2
1

As per the comment on the question:

Characters turning to: ÄÅçkıolÅÅçiÄp

That's what you get when you decode the character sequence ğşçkıolşşçiğp to bytes using UTF-8 and then encode those bytes to characters using ISO-8859-1 (or ISO-8859-9).

Modern browsers other than MSIE are smart enough to detect the response encoding when the charset is not specified in the HTTP Content-Type header of the response of the page returning the initial HTML. Thus, that it fails on MSIE can only mean that the proper charset is not been specified in the HTTP Content-Type header.

Adding the following line to very top of JSP page which is rendering the HTML code should fix it:

<%@ page pageEncoding="UTF-8" %>

It will set the response encoding and the content type header charset to the specified charset.

You can also add the following entry to your webapp's /WEB-INF/web.xml file to let it take effect on every JSP page of your webapp:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

Perhaps you're already doing it the right way, but you should also take into account that you're parsing the POST request parameters using the same charset. In the answer.jsp which you (ab)used as a JSON service, you should ensure that the following line is called before you access any request parameter:

request.setCharacterEncoding("UTF-8");
String nickname = request.getParameter("nickname");
// ...

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555