0

I am getting issue for polish characters in ajax call. In alert shown in below code, polish characters are not coming properly.

$.ajax({
        type: "GET",
        url: "/module/getAllApps.htm",
        encoding:"UTF-8",
        contentType:"application/x-www-form-urlencoded; charset=UTF-8",
        async: true,
        success : function(response) { 
            if(response != null && response != "" && response!= '' && response != 'null'){
                var appList = JSON.parse(response);
                for(var i=0; i<appList.length; i++){
                    var module = appList[i];
                    alert(module.title);
                 }
              }
        },
        error : function(e){
            console.log('Error: ' + e);
        }
    }); 

Below is method from Controller class

public void getAllApps(HttpServletRequest request, HttpServletResponse response){

    Gson gson = new Gson();
    List<Module> moduleList = moduleDao.getAllActiveModulesByDomain(domain.getDomainId());

    try {
        if(moduleList != null && moduleList.size()> 0){
            response.getWriter().print(gson.toJson(moduleList));
    } catch (Exception e) {
        e.printStackTrace();
    }
} 
1
  • How aren't they coming properly? Be specific. Commented Mar 3, 2015 at 12:07

1 Answer 1

1

Ensure that you're using the CharacterEncodingFilter, add the following in your web.xml

<filter>
    <filter-name>encoding-filter</filter-name>
    <filter-class>
        org.springframework.web.filter.CharacterEncodingFilter
    </filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encoding-filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

You can also make sure that your server is configured properly, for tomcat for instance adding URIEncoding to connector

<connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>

will specify the character encoding used to decode the URI. You should find an equivalent for your server

Finally, if you're problem persists, check that the decoding of your DB and your connection to the DB is also properly set

Sign up to request clarification or add additional context in comments.

1 Comment

did you cover first two points, there's also an encoding setup on the connection string for mysql an exemplary connection string, jdbc:mysql://localhost:3306/DB_Name?useUnicode=true&amp;characterEncoding=UTF-8 with this four points covered your encoding should be proper

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.