jquery ajax 请求 乱码

来源:互联网 发布:中小学远程教育软件 编辑:程序博客网 时间:2024/04/27 22:07


一、前台:解决中文提交乱码contentType:"application/x-www-form-urlencoded; charset=UTF-8"

$.ajax({url:xpcHomeLocation+'PlugInManageAction.do?operation=getMenus',dataType:"json",contentType:"application/x-www-form-urlencoded; charset=UTF-8", //解决中文提交乱码问题success:function(result){}})

二、后台:解决中文返回jsp页面乱码 response.setContentType("text/xml;charset=utf-8");//fix the Chinese messy code

 ObjectMapper objectMapper=new ObjectMapper();        Map<String,Object> result=new HashMap<String,Object>();        result.put("menus",StringUtils.join(menusNameList.toArray(),","));//use to autocomplete               String out=objectMapper.writeValueAsString(result);        response.setContentType("text/xml;charset=utf-8");//解决中文返回乱码问题        response.getWriter().print(out);

或者:json格式输出第二种方法

  1. JSONArray json = JSONArray.fromObject(SysList);//SysList是一个List  
  2. //      设置response的ContentType解决中文乱码  
  3.         response.setContentType("text/html;charset=UTF-8");  
  4.         response.getWriter().print(json.toString());  

三、取数据乱码:

1.首先不要用get提交而要用post  

2.然后encodeURI参数 

3.request.setCharacterEncoding("utf-8");

   response.setCharacterEncoding("utf-8");
   response.setContentType("application/x-json");

4.后台menu=URLDecoder.decode(menu,"utf-8");取出正确数据

$.ajax({url:xpcHomeLocation+'PlugInManageAction.do?operation=checkMenuExist',type:"post",dataType:"json",data:{"tab":encodeURI(tabName),"menu":encodeURI(menu),"menui18n":encodeURI(menui18n),"menuI18nTitle":encodeURI(realMenu)},contentType:"application/x-www-form-urlencoded; charset=UTF-8", success:function(result){},error{}



原创粉丝点击