你的位置:首页 > Java教程

[Java教程]ajax传输中文乱码解决方法


问题描述:

  我是在一个jsp页面有一个保存按钮,点击时会触发saveForm()的js函数,在saveForm()函数里经过校验后,会通过ajax发送数据请求,这样就不用通过提交表单来传输数据了,ajax估计就是这样的好处吧,目前对ajax还不太熟悉。

ajax传输乱码时的代码:

 1 function saveForm(){ 2   if(document.theformadd.onsubmit()){ 3     disableAllBtn(true); 4     j$.ajax({ 5       type:"get", 6       url:"add_form_do.jsp", 7       data:{ 8         problem_id : j$("#problem_id").val(), 9         product_id : "<%=product_id%>",10         productId : j$("#productId").val(),11         depart_id : j$("#depart_id").val(),12         fk_busi_id : j$("#fk_busi_id").val(),13         fk_type : j$("#fk_type").val(),14         fk_source : j$("#fk_source").val(),15         fk_info : j$("#fk_info").val(),16         fk_name : j$("#fk_name").val(),17         fk_bank_name : j$("#fk_bank_name").val(),18         fk_bank_acct : j$("#fk_bank_acct").val(),19         sk_name : j$("#sk_name").val(),20         sk_bank_name : j$("#sk_bank_name").val(),21         sk_bank_acct : j$("#sk_bank_acct").val(),22         fk_money : j$("#fk_money").val(),23         fk_summary : j$("#fk_summary").val(),24         fk_date : j$("#fk_date").val(),25         input_man : "<%=input_operatorCode%>"26       },27       success:function(ret){28         if(ret == 1) {29           sl_alert("保存成功!");30         }else{31           sl_alert(ret);32           33         }34 35         window.returnValue=true;36         window.close();37       38         39       }40     });41   }42 }

然后我们在add_form_do.jsp中获取数据

 1 <%@ page contentType="text/html; charset=GBK" import="java.math.*,com.enfo.intrust.intrust.vo.*,java.io.*, com.enfo.intrust.web.*,java.util.*,com.enfo.intrust.dao.*,com.enfo.intrust.intrust.*,com.enfo.intrust.tools.*,com.enfo.intrust.project.*" %> 2 <script type="text/javascript" src="<%=request.getContextPath()%>/ext2.0/ext-base.js"></script> 3 <script type="text/javascript" src="<%=request.getContextPath()%>/ext2.0/ext-all.js"></script> 4 <%@ include file="/includes/operator.inc" %> 5 <% 6 try{ 7   product_id = Utility.parseInt(Utility.trimNull(request.getParameter("product_id")),product_id); 8    9   UnpostwarrantLocal local = EJBFactory.getUnpostwarrant();10   11   Integer problem_id = Utility.parseInt(request.getParameter("problem_id"),new Integer(0));12   13   Integer depart_id = Utility.parseInt(request.getParameter("depart_id"),new Integer(0));    //部门14   Integer productId = Utility.parseInt(Utility.trimNull(request.getParameter("productId")),product_id);15   String fk_busi_id = Utility.trimNull(request.getParameter("fk_busi_id"));    //字典1206 费用16   String fk_type = Utility.trimNull(request.getParameter("fk_type"));    //付款方式 210317   String fk_source = Utility.trimNull(request.getParameter("fk_source"));    //付款依据 210418   String fk_info = Utility.trimNull(request.getParameter("fk_info"));    //票据号码19   String fk_name = Utility.trimNull(request.getParameter("fk_name"));    //付款单位20   String fk_bank_name = Utility.trimNull(request.getParameter("fk_bank_name"));    //付款银行名称21   String fk_bank_acct = Utility.trimNull(request.getParameter("fk_bank_acct"));    //付款银行账号22   String sk_name = Utility.trimNull(request.getParameter("sk_name"));    //收款单位23   String sk_bank_name = Utility.trimNull(request.getParameter("sk_bank_name"));    //收款银行名称24   String sk_bank_acct = Utility.trimNull(request.getParameter("sk_bank_acct"));    //收款银行账号25   BigDecimal fk_money = Utility.parseDecimal(Utility.trimNull(request.getParameter("fk_money")).replaceAll(",",""),new BigDecimal(0));    //金额26   String fk_summary = Utility.trimNull(request.getParameter("fk_summary"));    //备注27   Integer fk_date = Utility.parseInt(request.getParameter("fk_date"),new Integer(Utility.getCurrentDate()));    //要求付款日期28   29   local.setProblem_id(problem_id);30   //local.setProduct_id(product_id);31   local.setProduct_id(productId);32   local.setDepart_id(depart_id);33   local.setFk_busi_id(fk_busi_id);34   local.setFk_type(fk_type);35   local.setFk_source(fk_source);36   local.setFk_info(fk_info);37   local.setFk_name(fk_name);38   local.setFk_bank_name(fk_bank_name);39   local.setFk_bank_acct(fk_bank_acct);40   local.setSk_name(sk_name);41   local.setSk_bank_name(sk_bank_name);42   local.setSk_bank_acct(sk_bank_acct);43   local.setFk_money(fk_money);44   local.setFk_summary(fk_summary);45   local.setFk_date(fk_date);46   local.setInput_man(input_operatorCode);47   local.addFinacialcardInfoGuotou();48   out.clear();49   response.getWriter().write("1");50 }catch(Exception e){51   out.clear();52   response.getWriter().write(e.getMessage());53 }54 55 %>

 

 

这时候我接受到的数据会是中文乱码,不管传输方式是get,还是post都会中文乱码

解决方案:

  我们可以在传输时对数据重新编码,然后在接受数据时重新解码。其实乱码的问题就是编码格式冲突,导致解码的密钥对之前格式解析错误,导致乱码。在传输时,在需要传输中文数据前面加一个encodeURI()编码,例如:encodeURI(j$("#fk_info").val());在接受需要中文数据的前面加一个java.net.URLDecoder.decode(value, "UTF-8"),例如

String fk_bank_name = Utility.trimNull(request.getParameter("fk_bank_name")); 
String trans = java.net.URLDecoder.decode(fk_bank_name, "UTF-8");

具体修改后的全部代码如下:

修复后ajax传输的代码:

 1 function saveForm(){ 2   if(document.theformadd.onsubmit()){ 3     disableAllBtn(true); 4     j$.ajax({ 5       type:"get", 6       url:"add_form_do.jsp", 7       data:{ 8         problem_id : j$("#problem_id").val(), 9         product_id : "<%=product_id%>",10         productId : j$("#productId").val(),11         depart_id : j$("#depart_id").val(),12         fk_busi_id : j$("#fk_busi_id").val(),13         fk_type : j$("#fk_type").val(),14         fk_source : j$("#fk_source").val(),15         fk_info : encodeURI(j$("#fk_info").val()),16         fk_name : encodeURI(j$("#fk_name").val()),17         fk_bank_name : encodeURI(j$("#fk_bank_name").val()),18         fk_bank_acct : encodeURI(j$("#fk_bank_acct").val()),19         sk_name : encodeURI(j$("#sk_name").val()),20         sk_bank_name : encodeURI(j$("#sk_bank_name").val()),21         sk_bank_acct : encodeURI(j$("#sk_bank_acct").val()),22         fk_money : j$("#fk_money").val(),23         fk_summary : encodeURI(j$("#fk_summary").val()),24         fk_date : j$("#fk_date").val(),25         input_man : "<%=input_operatorCode%>"26       },27       success:function(ret){28         if(ret == 1) {29           sl_alert("保存成功!");30         }else{31           sl_alert(ret);32           33         }34 35         window.returnValue=true;36         window.close();37       38         39       }40     });41   }42 }

 

修复后add_form_do.jsp中获取数据:

 1 <%@ page contentType="text/html; charset=GBK" import="java.math.*,com.enfo.intrust.intrust.vo.*,java.io.*, com.enfo.intrust.web.*,java.util.*,com.enfo.intrust.dao.*,com.enfo.intrust.intrust.*,com.enfo.intrust.tools.*,com.enfo.intrust.project.*" %> 2 <script type="text/javascript" src="<%=request.getContextPath()%>/ext2.0/ext-base.js"></script> 3 <script type="text/javascript" src="<%=request.getContextPath()%>/ext2.0/ext-all.js"></script> 4 <%@ include file="/includes/operator.inc" %> 5 <% 6 try{ 7   product_id = Utility.parseInt(Utility.trimNull(request.getParameter("product_id")),product_id); 8    9   UnpostwarrantLocal local = EJBFactory.getUnpostwarrant();10   11   Integer problem_id = Utility.parseInt(request.getParameter("problem_id"),new Integer(0));12   13   Integer depart_id = Utility.parseInt(request.getParameter("depart_id"),new Integer(0));    //部门14   Integer productId = Utility.parseInt(Utility.trimNull(request.getParameter("productId")),product_id);15   String fk_busi_id = Utility.trimNull(request.getParameter("fk_busi_id"));    //字典1206 费用16   String fk_type = Utility.trimNull(request.getParameter("fk_type"));    //付款方式 210317   String fk_source = Utility.trimNull(request.getParameter("fk_source"));    //付款依据 210418   String fk_info = Utility.trimNull(request.getParameter("fk_info"));    //票据号码19   String fk_name = Utility.trimNull(request.getParameter("fk_name"));    //付款单位20   String fk_bank_name = Utility.trimNull(request.getParameter("fk_bank_name"));    //付款银行名称21   String fk_bank_acct = Utility.trimNull(request.getParameter("fk_bank_acct"));    //付款银行账号22   String sk_name = Utility.trimNull(request.getParameter("sk_name"));    //收款单位23   String sk_bank_name = Utility.trimNull(request.getParameter("sk_bank_name"));    //收款银行名称24   String sk_bank_acct = Utility.trimNull(request.getParameter("sk_bank_acct"));    //收款银行账号25   BigDecimal fk_money = Utility.parseDecimal(Utility.trimNull(request.getParameter("fk_money")).replaceAll(",",""),new BigDecimal(0));    //金额26   String fk_summary = Utility.trimNull(request.getParameter("fk_summary"));    //备注27   Integer fk_date = Utility.parseInt(request.getParameter("fk_date"),new Integer(Utility.getCurrentDate()));    //要求付款日期28   29   local.setProblem_id(problem_id);30   //local.setProduct_id(product_id);31   local.setProduct_id(productId);32   local.setDepart_id(depart_id);33   local.setFk_busi_id(fk_busi_id);34   local.setFk_type(fk_type);35   local.setFk_source(fk_source);36   local.setFk_info(java.net.URLDecoder.decode(fk_info, "UTF-8"));37   local.setFk_name(java.net.URLDecoder.decode(fk_name, "UTF-8"));38   local.setFk_bank_name(java.net.URLDecoder.decode(fk_bank_name, "UTF-8"));39   local.setFk_bank_acct(java.net.URLDecoder.decode(fk_bank_acct, "UTF-8"));40   local.setSk_name(java.net.URLDecoder.decode(sk_name, "UTF-8"));41   local.setSk_bank_name(java.net.URLDecoder.decode(sk_bank_name, "UTF-8"));42   local.setSk_bank_acct(java.net.URLDecoder.decode(sk_bank_acct, "UTF-8"));43   local.setFk_money(fk_money);44   local.setFk_summary(java.net.URLDecoder.decode(fk_summary, "UTF-8"));45   local.setFk_date(fk_date);46   local.setInput_man(input_operatorCode);47   local.addFinacialcardInfoGuotou();48   out.clear();49   response.getWriter().write("1");50 }catch(Exception e){51   out.clear();52   response.getWriter().write(e.getMessage());53 }54 55 %>

注意事项:

  如果是接受数据不是jsp页面,而是Java类的时候,只需要URLDecoder.decode(value, "UTF-8");来解码,然后导入相应的包。还有传输时可能需要两次编码encodeURI(encodeURI(j$("#fk_info").val())),具体原因是我们通过request.getParameter()来获取数据时就会进行一次解码操作,解码时不变。