


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!

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.