在Ajax调用波兰字符编码问题波兰、字符、问题、Ajax

2023-09-10 22:01:43 作者:爱吃貓de魚

我收到的问题在Ajax调用润色人物。在低于code示警戒,抛光的字符不来正常。

  $。阿贾克斯({
        键入:GET,
        网址:/module/getAllApps.htm
        编码:UTF-8,
        的contentType:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8,
        异步:真正的,
        成功:函数(响应){
            如果(响应=零和放大器;!&安培;!响应=&放大器;&安培;!响应=''和;&安培;!响应='空'){
                变种APPLIST = JSON.parse(响应);
                对于(VAR I = 0; I< appList.length;我++){
                    VAR模块= APPLIST [I]
                    警报(module.title);
                 }
              }
        },
        错误:函数(E){
            的console.log('错误:'+ E);
        }
    });
 

下面的方法从控制器类

 公共无效getAllApps(HttpServletRequest的请求,HttpServletResponse的响应){

    GSON GSON =新GSON();
    名单<模块> moduleList = moduleDao.getAllActiveModulesByDomain(domain.getDomainId());

    尝试 {
        如果(moduleList =空&安培;!&安培; moduleList.size()大于0){
            。response.getWriter()打印(gson.toJson(moduleList));
    }赶上(例外五){
        e.printStackTrace();
    }
}
 

解决方案

请确保您使用的是 CharacterEncodingFilter 的,加上在你的web.xml以下

 <滤光器>
    <过滤器名称>编码滤波器和LT; /过滤器名称>
    <滤波级>
        org.springframework.web.filter.CharacterEncodingFilter
    < /过滤器级>
    <初始化参数>
        <参数 - 名称>&编码LT; /参数 - 名称>
        <参数值> UTF-8< /参数值>
    < /初始化参数>
    <初始化参数>
        <参数 - 名称>&forceEncoding LT; /参数 - 名称>
        <参数值>真< /参数值>
    < /初始化参数>
< /滤光器>
<过滤器映射>
    <过滤器名称>编码滤波器和LT; /过滤器名称>
    < URL模式> / *< / URL模式>
< /过滤器映射>
 
jQuery Ajax

您也可以确保您的服务器已正确配置,为Tomcat例如增加的URIEncoding到连接器

 <连接器端口=8080协议=HTTP / 1.1connectionTimeout =20000redirectPort =8443的URIEncoding =UTF-8/>
 

将指定用于德丙的URI $ C $的字符编码​​。你应该找一个相当于为您的服务器

最后,如果你是问题仍然存在,请检查您的数据库和连接到数据库的解码也正确设置

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();
    }
} 

解决方案

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