2015-03-03 52 views
0

我在ajax調用中發現波蘭字符問題。在下面的代碼中顯示的警報中,波蘭字符未能正確顯示。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); 
     } 
    }); 

下面是從控制器類方法

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

怎麼不來他們正確?請明確點。 – 2015-03-03 12:07:38

回答

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> 

您也可以確保你的服務器配置正確,對於tomcat,例如將URIEncoding添加到連接器

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

將指定用於解碼URI的字符編碼。你應該找到適合您的服務器的等效

最後,如果你是問題仍然存在,請檢查您的數據庫和數據庫的連接的解碼也是正確設置

+0

在數據庫中,我能夠看到正確的值。 – RBP 2015-03-03 12:20:51

+0

你是否覆蓋了前兩點,mysql的連接字符串上還有一個編碼設置,它是一個示例連接字符串,jdbc:mysql:// localhost:3306/DB_Name?useUnicode = true & characterEncoding = UTF-8你的編碼應該是合適的 – 2015-03-03 12:23:40

+0

okie.It通過添加過濾器代碼顯示正確的值。 – RBP 2015-03-03 12:27:38