Home Web Front-end JS Tutorial How to solve the Chinese garbled code of AJAX POST data

How to solve the Chinese garbled code of AJAX POST data

Mar 05, 2018 pm 04:56 PM
ajax post Garbled characters

This article mainly shares with you how to solve the Chinese garbled code of AJAX POST data. The front-end uses encodeURI for encoding. I hope it can help everyone.

var param = encodeURI(param);
$.ajax({
        url: 'url',
        methodtype: "POST",
        async: false,
        timeout: 60000,
        contentType: "application/json",
        data: {'param':param},
        success: function(data) {

        },
        error: function(data) {

        }
    });
Copy after login

Backend java.net.URLDecoder for decoding
Encoding and decoding tools

import java.io.UnsupportedEncodingException;import java.net.URLDecoder;import java.net.URLEncoder;import org.apache.commons.codec.DecoderException;import org.apache.commons.codec.binary.Base64;import org.apache.commons.codec.binary.Hex;import org.apache.commons.lang3.StringEscapeUtils;/**
 * 封装各种格式的编码解码工具类.
 * 1.Commons-Codec的 hex/base64 编码
 * 2.自制的base62 编码
 * 3.Commons-Lang的xml/html escape
 * 4.JDK提供的URLEncoder
 * 
 */public class Encodes {

    private static final String DEFAULT_URL_ENCODING = "UTF-8";    private static final char[] BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();    /**
     * Hex编码.
     */
    public static String encodeHex(byte[] input) {        return new String(Hex.encodeHex(input));
    }    /**
     * Hex解码.
     */
    public static byte[] decodeHex(String input) {        try {            return Hex.decodeHex(input.toCharArray());
        } catch (DecoderException e) {            throw Exceptions.unchecked(e);
        }
    }    /**
     * Base64编码.
     */
    public static String encodeBase64(byte[] input) {        return new String(Base64.encodeBase64(input));
    }    /**
     * Base64编码.
     */
    public static String encodeBase64(String input) {        try {            return new String(Base64.encodeBase64(input.getBytes(DEFAULT_URL_ENCODING)));
        } catch (UnsupportedEncodingException e) {            return "";
        }
    }//  /**//   * Base64编码, URL安全(将Base64中的URL非法字符'+'和'/'转为'-'和'_', 见RFC3548).//   *///  public static String encodeUrlSafeBase64(byte[] input) {//      return Base64.encodeBase64URLSafe(input);//  }

    /**
     * Base64解码.
     */
    public static byte[] decodeBase64(String input) {        return Base64.decodeBase64(input.getBytes());
    }    /**
     * Base64解码.
     */
    public static String decodeBase64String(String input) {        try {            return new String(Base64.decodeBase64(input.getBytes()), DEFAULT_URL_ENCODING);
        } catch (UnsupportedEncodingException e) {            return "";
        }
    }    /**
     * Base62编码。
     */
    public static String encodeBase62(byte[] input) {        char[] chars = new char[input.length];        for (int i = 0; i < input.length; i++) {
            chars[i] = BASE62[((input[i] & 0xFF) % BASE62.length)];
        }        return new String(chars);
    }    /**
     * Html 转码.  例如将 < 转成 &lt
     */
    public static String escapeHtml(String html) {        return StringEscapeUtils.escapeHtml4(html);
    }    /**
     * Html 解码.
     */
    public static String unescapeHtml(String htmlEscaped) {        return StringEscapeUtils.unescapeHtml4(htmlEscaped);
    }    /**
     * Xml 转码.
     */
    public static String escapeXml(String xml) {        return StringEscapeUtils.escapeXml10(xml);
    }    /**
     * Xml 解码.
     */
    public static String unescapeXml(String xmlEscaped) {        return StringEscapeUtils.unescapeXml(xmlEscaped);
    }    /**
     * URL 编码, Encode默认为UTF-8. 
     */
    public static String urlEncode(String part) {        try {            return URLEncoder.encode(part, DEFAULT_URL_ENCODING);
        } catch (UnsupportedEncodingException e) {            throw Exceptions.unchecked(e);
        }
    }    /**
     * URL 解码, Encode默认为UTF-8. 
     */
    public static String urlDecode(String part) {        try {            return URLDecoder.decode(part, DEFAULT_URL_ENCODING);
        } catch (UnsupportedEncodingException e) {            throw Exceptions.unchecked(e);
        }
    }
}
Copy after login

Related recommendations:

Use the php curl_setopt() function to capture web pages A simple example with POST data

PHP implementation code for converting POST data into string

Three methods for php to obtain POST data Detailed explanation of examples

The above is the detailed content of How to solve the Chinese garbled code of AJAX POST data. 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