Polish Character encoding issue in ajax call

2019-09-08 04:39发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-09-08 05:27

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

查看更多
登录 后发表回答