Solution to the Problem of Chinese garbled Characters During Ajax transmission: Chinese garbled Characters During ajax Transmission

Source: Internet
Author: User
Tags saveform

Solution to the Problem of Chinese garbled Characters During Ajax transmission: Chinese garbled Characters During ajax Transmission

AJAX Introduction

AJAX = Asynchronous JavaScript and XML (Asynchronous JavaScript and XML ).

AJAX is not a new programming language, but a new method that uses existing standards.

AJAX is the art of exchanging data with the server and updating some webpages without reloading the entire page.

ADescription of Chinese garbled Characters During jax transmission:

I have a Save button on a jsp page. When I click it, the JavaScript function of saveForm () is triggered. After verification in the saveForm () function, the data request is sent through ajax, in this way, you do not need to submit a form to transmit data. ajax is probably the benefit of this. Currently, you are not familiar with ajax.

Code for transmitting garbled characters through ajax:

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_nam E: 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_a Lert ("saved successfully! ") ;}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 ="/DES/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); // Department Integer productId = Utility. parseInt (Utility. trimNull (request. getParameter ("productId"), product_id); String fk_busi_id = Utility. trimNull (request. getParameter ("fk_busi_id"); // dictionary 1206 cost String fk_type = Utility. trimNull (request. getParameter ("fk_type"); // payment method 2103 String fk_source = Utility. trimNull (request. getParameter ("fk_source"); // payment is based on 2104 String fk_info = Utility. trimNull (request. getParameter ("fk_info"); // bill number String fk_name = Utility. trimNull (request. getParameter ("fk_name"); // Payment Unit String fk_bank_name = Utility. trimNull (request. getParameter ("fk_bank_name"); // payment bank name String fk_bank_acct = Utility. trimNull (request. getParameter ("fk_bank_acct"); // payment bank account String sk_name = Utility. trimNull (request. getParameter ("sk_name"); // receipt unit String sk_bank_name = Utility. trimNull (request. getParameter ("sk_bank_name"); // beneficiary 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); // The value String fk_summary = Utility. trimNull (request. getParameter ("fk_summary"); // remarks Integer fk_date = Utility. parseInt (request. getParameter ("fk_date"), new Integer (Utility. getCurrentDate (); // requires the payment date 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 () ;}%>

At this time, the data I received will contain Chinese garbled characters, no matter whether the transmission method is get or post, it will contain Chinese garbled characters.

Solution:

We can re-encode the data during transmission and re-Decode it when receiving the data. In fact, the garbled problem is that the encoding format conflicts, leading to incorrect parsing of the decoded key pair, leading to garbled code. During transmission, add an encodeURI () encoding before transmitting Chinese data, for example, encodeURI (j $ ("# fk_info "). val (); add a java.net. URLDecoder. decode (value, "UTF-8"), such
String fk_bank_name = Utility. trimNull (request. getParameter ("fk_bank_name"); String trans = java.net. URLDecoder. decode (fk_bank_name, "UTF-8 ");
The modified code is as follows:

The code transmitted by ajax after the fix:

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 "). va L (), 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 (" saved successfully! ") ;}Else {sl_alert (ret) ;}window. returnValue = true; window. close ();}});}}

After the fix, add_form_do.jsp obtains the data:

<% @ 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 ="/DES/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); // Department Integer productId = Utility. parseInt (Utility. trimNull (request. getParameter ("productId"), product_id); String fk_busi_id = Utility. trimNull (request. getParameter ("fk_busi_id"); // dictionary 1206 cost String fk_type = Utility. trimNull (request. getParameter ("fk_type"); // payment method 2103 String fk_source = Utility. trimNull (request. getParameter ("fk_source"); // payment is based on 2104 String fk_info = Utility. trimNull (request. getParameter ("fk_info"); // bill number String fk_name = Utility. trimNull (request. getParameter ("fk_name"); // Payment Unit String fk_bank_name = Utility. trimNull (request. getParameter ("fk_bank_name"); // payment bank name String fk_bank_acct = Utility. trimNull (request. getParameter ("fk_bank_acct"); // payment bank account String sk_name = Utility. trimNull (request. getParameter ("sk_name"); // receipt unit String sk_bank_name = Utility. trimNull (request. getParameter ("sk_bank_name"); // beneficiary 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); // The value String fk_summary = Utility. trimNull (request. getParameter ("fk_summary"); // remarks Integer fk_date = Utility. parseInt (request. getParameter ("fk_date"), new Integer (Utility. getCurrentDate (); // requires the payment date 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 () ;}%>

Note:

If it is to accept the data is not jsp page, but Java class, only need URLDecoder. decode (value, "UTF-8"); to decode, and then import the corresponding package. You may also need to encode encodeURI (j $ ("# fk_info") twice during transmission "). val (), the specific reason is that we use request. when getParameter () is used to obtain data, a decoding operation will be performed, and the decoding will not change.

The above is a solution to the problem of Chinese garbled characters in Ajax transmission introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.