Home Web Front-end JS Tutorial Solution to the problem of Ajax transmission of Chinese garbled characters

Solution to the problem of Ajax transmission of Chinese garbled characters

Jan 13, 2018 am 11:42 AM
ajax 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.

Introduction to AJAX

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();
}
});
}
}
Copy after login

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());
}
%>
Copy after login

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();
}
});
}
}
Copy after login

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());
}
%>
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve Chinese garbled characters in Linux How to solve Chinese garbled characters in Linux Feb 21, 2024 am 10:48 AM

The Linux Chinese garbled problem is a common problem when using Chinese character sets and encodings. Garbled characters may be caused by incorrect file encoding settings, system locale not being installed or set, and terminal display configuration errors, etc. This article will introduce several common workarounds and provide specific code examples. 1. Check the file encoding setting. Use the file command to view the file encoding. Use the file command in the terminal to view the encoding of the file: file-ifilename. If there is "charset" in the output

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

How to solve the problem of garbled characters when importing Chinese data into Oracle? How to solve the problem of garbled characters when importing Chinese data into Oracle? Mar 10, 2024 am 09:54 AM

Title: Methods and code examples to solve the problem of garbled characters when importing Chinese data into Oracle. When importing Chinese data into Oracle database, garbled characters often appear. This may be due to incorrect database character set settings or encoding conversion problems during the import process. . In order to solve this problem, we can take some methods to ensure that the imported Chinese data can be displayed correctly. The following are some solutions and specific code examples: 1. Check the database character set settings In the Oracle database, the character set settings are

Tips for dealing with garbled Chinese file names in PHP Tips for dealing with garbled Chinese file names in PHP Feb 27, 2024 pm 02:18 PM

Tips for dealing with garbled Chinese file names in PHP During the development process, we often encounter the problem of garbled Chinese file names, especially when processing files uploaded by users. In PHP, how to correctly handle garbled file names is a common and important problem. This article will introduce some techniques for dealing with garbled Chinese file names and provide specific code examples to help readers better deal with this challenge. Problem description: When users upload files, the Chinese file names sometimes appear to be garbled. This is because different operating systems and browsers

Methods and detailed analysis to solve the problem of garbled characters in some win11 software Methods and detailed analysis to solve the problem of garbled characters in some win11 software Jan 30, 2024 pm 03:54 PM

Many users found that their personal software was garbled after upgrading the win11 system. So how to solve this problem? Now let the editor carefully introduce to users the analysis of garbled code problems in some software in win11. Analysis of garbled characters in some software in win11 1. Click the search box in the taskbar in the lower left corner and enter control panel to open it. 3. Click on the area. 5. Then uncheck the small box for beta version in the window, and finally restart the computer to solve the problem.

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

How to deal with garbled characters in Linux terminal How to deal with garbled characters in Linux terminal Mar 20, 2024 pm 03:12 PM

How to deal with the problem of garbled characters in the Linux terminal. When using the Linux system, sometimes the text displayed in the terminal will be garbled. This brings inconvenience to us when using the terminal and needs to be dealt with in time. This article will introduce how to deal with some common Linux terminal garbled problems, and provide specific code examples. Problem 1: Garbled Chinese characters on the terminal. Garbled Chinese characters on the terminal are usually caused by incorrect character encoding settings on the terminal. We can solve this problem by modifying the terminal's character encoding settings. #View the current terminal

How to solve the problem of garbled characters displayed on Win11 when booting? Two solutions to the garbled characters displayed on Win11 boot How to solve the problem of garbled characters displayed on Win11 when booting? Two solutions to the garbled characters displayed on Win11 boot Feb 29, 2024 pm 12:16 PM

Win11 is Microsoft's latest operating system, but some users may encounter the problem of displaying garbled characters when booting, which will affect the normal use of the system. This article will introduce some methods to solve this problem. Method 1: 1. Press the [Win+S] key combination, or click the [Search icon] next to the start icon on the taskbar. In the opened Windows search, enter [Control Panel] in the search box, and then click [Open]. The best matching control panel application out of the control panel window; 2. In the control panel window, switch to the [Category] view mode, and then click [Clock and Zone-Region]; 3. In the zone window, switch to the [Management] tab, and then click [Change] System Regional Settings]; 4. [Uncheck] Beta version: Use Unicode

See all articles