


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!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
