ajax调用中的波兰字符编码问题

时间:2015-03-03 11:52:48

标签: jquery ajax spring-mvc

我在ajax调用中遇到波兰字符的问题。在下面代码中显示的警告中,抛光字符未正确显示。

$.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);
        }
    }); 

以下是Controller类

的方法
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 个答案:

答案 0 :(得分:1)

确保您使用 CharacterEncodingFilter ,在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>

您还可以确保您的服务器配置正确,例如将tomIEncoding添加到连接器

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

将指定用于解码URI的字符编码。您应该找到服务器的等效项

最后,如果您的问题仍然存在,请检查您的数据库解码以及您与数据库的连接是否也已正确设置