Home > Article > Web Front-end > Solution to the problem of Ajax transmission of Chinese garbled characters
This article mainly introduces the relevant information on the solution to the Ajax transmission Chinese garbled code problem. It is very good and has the value of reference and learning ajax. Friends who are interested in ajax can refer to this article.
AJAX = Asynchronous JavaScript and XML (Asynchronous JavaScript and XML ).
AJAX is not a new programming language, but a new way of using existing standards.
AJAX is the art of exchanging data with a server and updating parts of a web page without reloading the entire page.
ajax transmission Chinese garbled problem description:
I am in a The jsp page has a save button. When clicked, the js function of saveForm() will be triggered. After verification in the saveForm() function, the data request will be sent through ajax, so that there is no need to submit the form to transmit the data. Ajax is estimated to be The advantage of this is that I am not very familiar with ajax at the moment.
The code when ajax transmits garbled characters:
function saveForm(){ if(document.theformadd.onsubmit()){ disableAllBtn(true); j$.ajax({ type:"get", url:"add_form_do.jsp", data:{ problem_id : j$("#problem_id").val(), product_id : "<%=product_id%>", productId : j$("#productId").val(), depart_id : j$("#depart_id").val(), fk_busi_id : j$("#fk_busi_id").val(), fk_type : j$("#fk_type").val(), fk_source : j$("#fk_source").val(), fk_info : j$("#fk_info").val(), fk_name : j$("#fk_name").val(), fk_bank_name : j$("#fk_bank_name").val(), fk_bank_acct : j$("#fk_bank_acct").val(), sk_name : j$("#sk_name").val(), sk_bank_name : j$("#sk_bank_name").val(), sk_bank_acct : j$("#sk_bank_acct").val(), fk_money : j$("#fk_money").val(), fk_summary : j$("#fk_summary").val(), fk_date : j$("#fk_date").val(), input_man : "<%=input_operatorCode%>" }, success:function(ret){ if(ret == 1) { sl_alert("保存成功!"); }else{ sl_alert(ret); } window.returnValue=true; window.close(); } }); } }
Then we get the data in add_form_do.jsp
<%@ 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.*" %> <script type="text/javascript" src="<%=request.getContextPath()%>/ext2.0/ext-base.js"></script> <script type="text/javascript" src="<%=request.getContextPath()%>/ext2.0/ext-all.js"></script> <%@ include file="/includes/operator.inc" %> <% try{ product_id = Utility.parseInt(Utility.trimNull(request.getParameter("product_id")),product_id); UnpostwarrantLocal local = EJBFactory.getUnpostwarrant(); Integer problem_id = Utility.parseInt(request.getParameter("problem_id"),new Integer(0)); Integer depart_id = Utility.parseInt(request.getParameter("depart_id"),new Integer(0)); //部门 Integer productId = Utility.parseInt(Utility.trimNull(request.getParameter("productId")),product_id); String fk_busi_id = Utility.trimNull(request.getParameter("fk_busi_id")); //字典1206 费用 String fk_type = Utility.trimNull(request.getParameter("fk_type")); //付款方式 2103 String fk_source = Utility.trimNull(request.getParameter("fk_source")); //付款依据 2104 String fk_info = Utility.trimNull(request.getParameter("fk_info")); //票据号码 String fk_name = Utility.trimNull(request.getParameter("fk_name")); //付款单位 String fk_bank_name = Utility.trimNull(request.getParameter("fk_bank_name")); //付款银行名称 String fk_bank_acct = Utility.trimNull(request.getParameter("fk_bank_acct")); //付款银行账号 String sk_name = Utility.trimNull(request.getParameter("sk_name")); //收款单位 String sk_bank_name = Utility.trimNull(request.getParameter("sk_bank_name")); //收款银行名称 String sk_bank_acct = Utility.trimNull(request.getParameter("sk_bank_acct")); //收款银行账号 BigDecimal fk_money = Utility.parseDecimal(Utility.trimNull(request.getParameter("fk_money")).replaceAll(",",""),new BigDecimal(0)); //金额 String fk_summary = Utility.trimNull(request.getParameter("fk_summary")); //备注 Integer fk_date = Utility.parseInt(request.getParameter("fk_date"),new Integer(Utility.getCurrentDate())); //要求付款日期 local.setProblem_id(problem_id); //local.setProduct_id(product_id); local.setProduct_id(productId); local.setDepart_id(depart_id); local.setFk_busi_id(fk_busi_id); local.setFk_type(fk_type); local.setFk_source(fk_source); local.setFk_info(fk_info); local.setFk_name(fk_name); local.setFk_bank_name(fk_bank_name); local.setFk_bank_acct(fk_bank_acct); local.setSk_name(sk_name); local.setSk_bank_name(sk_bank_name); local.setSk_bank_acct(sk_bank_acct); local.setFk_money(fk_money); local.setFk_summary(fk_summary); local.setFk_date(fk_date); local.setInput_man(input_operatorCode); local.addFinacialcardInfoGuotou(); out.clear(); response.getWriter().write("1"); }catch(Exception e){ out.clear(); response.getWriter().write(e.getMessage()); } %>
The data I receive at this time will be Chinese garbled, no matter the transmission method is get or post, it will be Chinese garbled
Solution:
#We can re-encode the data when transmitting, and then re-decode when receiving the data. In fact, the problem of garbled codes is that the encoding format conflict causes the decoded key pair to have a previous format parsing error, resulting in garbled codes. During transmission, add an encodeURI() encoding in front of the Chinese data that needs to be transmitted, for example: encodeURI(j$("#fk_info").val()); add a java.net.URLDecoder in front of the Chinese data that needs to be received. decode(value, "UTF-8"), for example
String fk_bank_name = Utility.trimNull(request.getParameter("fk_bank_name")); String trans = java.net.URLDecoder.decode(fk_bank_name, "UTF-8" );
The specific modified code is as follows:
The repaired ajax transmission code:
function saveForm(){ if(document.theformadd.onsubmit()){ disableAllBtn(true); j$.ajax({ type:"get", url:"add_form_do.jsp", data:{ problem_id : j$("#problem_id").val(), product_id : "<%=product_id%>", productId : j$("#productId").val(), depart_id : j$("#depart_id").val(), fk_busi_id : j$("#fk_busi_id").val(), fk_type : j$("#fk_type").val(), fk_source : j$("#fk_source").val(), fk_info : encodeURI(j$("#fk_info").val()), fk_name : encodeURI(j$("#fk_name").val()), fk_bank_name : encodeURI(j$("#fk_bank_name").val()), fk_bank_acct : encodeURI(j$("#fk_bank_acct").val()), sk_name : encodeURI(j$("#sk_name").val()), sk_bank_name : encodeURI(j$("#sk_bank_name").val()), sk_bank_acct : encodeURI(j$("#sk_bank_acct").val()), fk_money : j$("#fk_money").val(), fk_summary : encodeURI(j$("#fk_summary").val()), fk_date : j$("#fk_date").val(), input_man : "<%=input_operatorCode%>" }, success:function(ret){ if(ret == 1) { sl_alert("保存成功!"); }else{ sl_alert(ret); } window.returnValue=true; window.close(); } }); } }
Get the data in add_form_do.jsp after repair:
<%@ 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.*" %> <script type="text/javascript" src="<%=request.getContextPath()%>/ext2.0/ext-base.js"></script> <script type="text/javascript" src="<%=request.getContextPath()%>/ext2.0/ext-all.js"></script> <%@ include file="/includes/operator.inc" %> <% try{ product_id = Utility.parseInt(Utility.trimNull(request.getParameter("product_id")),product_id); UnpostwarrantLocal local = EJBFactory.getUnpostwarrant(); Integer problem_id = Utility.parseInt(request.getParameter("problem_id"),new Integer(0)); Integer depart_id = Utility.parseInt(request.getParameter("depart_id"),new Integer(0)); //部门 Integer productId = Utility.parseInt(Utility.trimNull(request.getParameter("productId")),product_id); String fk_busi_id = Utility.trimNull(request.getParameter("fk_busi_id")); //字典1206 费用 String fk_type = Utility.trimNull(request.getParameter("fk_type")); //付款方式 2103 String fk_source = Utility.trimNull(request.getParameter("fk_source")); //付款依据 2104 String fk_info = Utility.trimNull(request.getParameter("fk_info")); //票据号码 String fk_name = Utility.trimNull(request.getParameter("fk_name")); //付款单位 String fk_bank_name = Utility.trimNull(request.getParameter("fk_bank_name")); //付款银行名称 String fk_bank_acct = Utility.trimNull(request.getParameter("fk_bank_acct")); //付款银行账号 String sk_name = Utility.trimNull(request.getParameter("sk_name")); //收款单位 String sk_bank_name = Utility.trimNull(request.getParameter("sk_bank_name")); //收款银行名称 String sk_bank_acct = Utility.trimNull(request.getParameter("sk_bank_acct")); //收款银行账号 BigDecimal fk_money = Utility.parseDecimal(Utility.trimNull(request.getParameter("fk_money")).replaceAll(",",""),new BigDecimal(0)); //金额 String fk_summary = Utility.trimNull(request.getParameter("fk_summary")); //备注 Integer fk_date = Utility.parseInt(request.getParameter("fk_date"),new Integer(Utility.getCurrentDate())); //要求付款日期 local.setProblem_id(problem_id); //local.setProduct_id(product_id); local.setProduct_id(productId); local.setDepart_id(depart_id); local.setFk_busi_id(fk_busi_id); local.setFk_type(fk_type); local.setFk_source(fk_source); local.setFk_info(java.net.URLDecoder.decode(fk_info, "UTF-8")); local.setFk_name(java.net.URLDecoder.decode(fk_name, "UTF-8")); local.setFk_bank_name(java.net.URLDecoder.decode(fk_bank_name, "UTF-8")); local.setFk_bank_acct(java.net.URLDecoder.decode(fk_bank_acct, "UTF-8")); local.setSk_name(java.net.URLDecoder.decode(sk_name, "UTF-8")); local.setSk_bank_name(java.net.URLDecoder.decode(sk_bank_name, "UTF-8")); local.setSk_bank_acct(java.net.URLDecoder.decode(sk_bank_acct, "UTF-8")); local.setFk_money(fk_money); local.setFk_summary(java.net.URLDecoder.decode(fk_summary, "UTF-8")); local.setFk_date(fk_date); local.setInput_man(input_operatorCode); local.addFinacialcardInfoGuotou(); out.clear(); response.getWriter().write("1"); }catch(Exception e){ out.clear(); response.getWriter().write(e.getMessage()); } %>
If the data is not a jsp page, but a Java class, you only need URLDecoder.decode(value, " UTF-8"); to decode and then import the corresponding package. It may also be necessary to encode encodeURI(encodeURI(j$("#fk_info").val())) twice during transmission. The specific reason is that when we obtain data through request.getParameter(), we will perform a decoding operation. Decoding Time remains unchanged.
Related recommendations:
Important knowledge points that must be mastered in AJAX applications
ajax method to implement the registration function
Ajax combined with php to implement secondary linkage instance method
The above is the detailed content of Solution to the problem of Ajax transmission of Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!