Polish character encoding issue in ajax call

I get a problem for polish characters in ajax call. In the warning shown in the code below, the Polish character does not fit 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);
        }
    }); 

The following is a method from the 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();
    }
} 
0
source share
1 answer

Make sure you are using CharacterEncodingFilter, add the following to 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 correctly for tomcat, for example, adding URIEncoding to the connector

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

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

, , , .

+1

Source: https://habr.com/ru/post/1584203/


All Articles