我在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();
}
}
确保您正在使用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的字符编码。您应该为您的服务器找到一个等效项
最后,如果问题仍然存在,请检查数据库的解码以及与数据库的连接是否也已正确设置
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句