前台代码
$.ajax({
        url:'/test/test.do',
        type:"POST",
        data:"test="+test,  //test是中文字符
        error: function(msg){
            alert('Error loading XML document'+msg);
        },
        success: function(data)
        {
            alert(data);
         }
})后台代码test.javapublic class UsbkeyWriteCheckUsbkeyInfo extends Action {
······
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

HttpSession session=request.getSession();

UsbkeyForm  usbkeyForm = (UsbkeyForm)form;
        System.out.println(usbkeyForm.getJgdm());
        
        
        
        String test= request.getParameter("test");
              
        String responseText="1";
        PrintWriter out=response.getWriter();
        out.println(responseText);
        return super.execute(mapping, form, request, response);
}



}我后台获取的test是乱码 请问这个是什么情况 小弟没有搞JAVA请大牛帮帮忙

解决方案 »

  1.   

    我和你碰到过一样的情况,是网上查的方法。
    我的jsp页面编码为gb2312,ajax传参数时这么传encodeURIComponent(encodeURIComponent(test))
    action对收到的数据这么处理:
    URLDecoder.decode(URLDecoder.decode(test,"utf-8"), "gb2312");
    可以取消乱码,具体原理也不是很清楚,只知道jquery默认的编码是utf-8的
      

  2.   


    乱码就要找编码方式!  java默认的就是gb2312
      

  3.   

    Strut2 + MySql + Hibernate + ajax(jQuery)中文乱码解决办法:
    1. 网页页面使用utf-8
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">2. ajax默认使用utf-8传输数据, 但最好使用post, 不要使用get3. Struts2中使用UTF-8.
    3.1 在struts.properties中加上: struts.i18n.encoding=UTF-8
    [3.2 使用Filter: 从页面中传输数据到Struts2, 需要使用过滤器把数据在交给struts2前转换成utf-8的: http://stevieliu.blogchina.com/stevieliu/6223098.html]
    试过不使用3.2照常可以收到中文.4. 数据库也要使用utf-8, 在建表时设置好.
    http://imysql.cn/charset_tips5. hibernate.cf.xml配置文件中与数据库的连接符串也要标明使用的编码为utf-8: <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mlm?useUnicode=true&amp;characterEncoding=UTF-8</property>
      

  4.   

    jquery 编码是utf-8的 而且不能更改...
    所以如果后台编码不是utf-8的话就不要使用jquery发ajax了, 
    encodeURIComponent(encodeURIComponent(test))  这是是把汉字转化成 //2345这种形式来传递这样处理可以解决乱码的问题 但是好像要处理特殊字符才能,不然后特殊字符会出问题
      

  5.   

     那把后台的编码改成utf-8 可以吗,可以使用Jquery了吗?
      

  6.   

    可以自己写一个过滤器,把页面上内容全都设置为UTF-8
      

  7.   

    LZ,你试试把 type: "post" 改成 type: "Get"  看能否解决乱码问题,如果不行再在Action中加入:
    request.CharacterEncoding("UTF-8");如果能解决问题的话,换回POST,在Action中相应的加入如下代码: 
    String test = request.getParameter("test");
    //Action收到请求参数默认是以老外的ISO8859-1来解码的
    test = new String(test.getBytes("ISO8859-1"),"UTF-8");
     就可以解决乱码了。
    当然设置一个全局的中文乱码过滤器是最好的选择,4楼的filter似乎还有不妥之处就是POST请求乱码是如何解决的?
      

  8.   

    request.setCharacterEncoding("UTF-8");不好意思