zoukankan      html  css  js  c++  java
  • ajax中url赋json格式的值时发生中文乱码的相关问题

      具体流程:转入到jsp界面时会加载ajax,ajax转到url时传带hide在jsp界面的值titleString,其来源见下面的代码。

    String title=new String("\"日志模块访问统计分析图\"");
    String subtitle=new String("\""+df.format(startDateTemp).toString()+" 至 "+df.format(endDateTemp).toString()+"各模块访问饼状图"+"\"");
    titleMap.put("title", title);
    titleMap.put("subtitle", subtitle);            
    yString=yAxisMap.toString();
    titleString=titleMap.toString();

      此处,在加中文字符串时必须要用\"引号的转义字符串括起,其格式应该如下:{title="日志模块访问统计分析图", subtitle="当日服务器及各模块访问折线图"},而不是{title=日志模块访问统计分析图, subtitle=当日服务器及各模块访问折线图}

    $(function(){  
                $.ajax({  
                   type: 'POST',  
                   dataType: 'JSON',
                   url: "logStatistic/statisticLog.action?titleString="+$("#titleString").val(),  
                       success : function(result){
                           
                        }    
                }); 
            });

      ajax进入action中时,需用request.getParameter("titleString")取到titleString,然后titleString.getBytes("iso-8859-1"), "UTF-8")进行编码处理。此处可以看出前台界面的编码方式。但是,在jsp界面中,我已用pageEncoding="UTF-8"确定,为什么此处要为iso-8859-1?我用谷歌F12观察前台界面数据是没有乱码的。

      

      那应该是后台action中接收的编码方式是iso-8859-1。

      在action中打断点,观察到若用Map temp1Map = JSONObject.fromObject(titleString)得到为{title="日志模块访问统计分析图", subtitle="2014å¹´03月25日 至 2014å¹´03月26日各模块访问饼状图"},是乱码。所以此处要求编码方式的一些处理。即,titleString=request.getParameter("titleString"),titleString.getBytes("iso-8859-1"), "UTF-8"),将其转为UTF-8格式。

      request 为protected HttpServletRequest request;// 请求的request对象。

    public String statisticLog() {
            Map tempMap = JSONObject.fromObject(yString);
            tempMap.putAll(JSONObject.fromObject(xString));
            try {
                titleString = new String(request.getParameter("titleString").getBytes("iso-8859-1"), "UTF-8"); 
                tempMap.putAll(JSONObject.fromObject(titleString));
            } catch (Exception e) {
                e.printStackTrace();
            }
            chartMap=tempMap;
            chartMap.put("timeType", timeType);
            return SUCCESS;
        }

      网上还有使用escape(或encodeURI)的方法的,我尚未验证,贴出地址:http://www.cnblogs.com/qiuyi21/articles/1089555.html,感觉比较麻烦。

  • 相关阅读:
    Coroutine in Java
    常见的开源日志(包括分布式)
    深入理解 Java G1 垃圾收集器--转
    卷积神经网络——本质上是在利用卷积做特征压缩,然后再全连接
    神经网络和反向传播算法——反向传播算法本质上是随机梯度下降,链式求导法则而来的
    LSTM入门学习——结合《LSTM模型》文章看
    LSTM入门学习——本质上就是比RNN的隐藏层公式稍微复杂了一点点而已
    LSTM模型
    syslog介绍-CS架构来采集系统日志
    NetFlow是一种数据交换方式,提供网络流量的会话级视图,记录下每个TCP/IP事务的信息
  • 原文地址:https://www.cnblogs.com/byonecry/p/3622477.html
Copyright © 2011-2022 走看看