Home >Web Front-end >Front-end Q&A >jquery request url parameter garbled problem

jquery request url parameter garbled problem

WBOY
WBOYOriginal
2023-05-23 11:28:07643browse

In development, we often use Ajax to make data requests, or use jQuery to send GET or POST requests. Sometimes, we encounter the problem of garbled characters when passing Chinese parameters. In this article, we will explore the jQuery request URL parameter garbled problem and provide some solutions.

  1. Cause of the problem

When the browser sends a request to the server, the URL parameters will be encoded. Under different encoding methods, the conversion results of Chinese characters will also be different. For example, under GB2312 encoding, the encoding corresponding to the character "中" is "D6D0", and under UTF-8 encoding, the encoding corresponding to the character "中" is "E4B8AD".

When the browser sends a request in GET mode, the parameters will be automatically encoded. When using the jQuery.ajax method, if the parameters contain Chinese characters, jQuery will automatically encode these characters. Usually this encoding The format is UTF-8. However, when the server receives the request, if the character set encoding is not specified, the default encoding method will be used to process the parameters and the parameters will be decoded, so the Chinese parameters will be garbled.

  1. Solution

To solve the problem of garbled Chinese parameters, different processing needs to be carried out according to different situations.

2.1. Modify the server-side encoding method

In some cases, we can directly modify the encoding method on the server side and specify the use of UTF-8 encoding to avoid the problem of garbled Chinese parameters. For example, in the Java JSP code, you can add the following code:

request.setCharacterEncoding("UTF-8");

2.2. Modify the jQuery encoding method

In jQuery.ajax, you can modify the encoding by setting the values ​​of the parameters processData and contentType Way. Set processData to false to indicate that the data encoding is handled by yourself; set contentType to application/x-www-form-urlencoded;charset=utf-8 to indicate that the data encoding method is UTF-8.

$.ajax({
    url: 'xxx',
    data: {
        name: '中文'
    },
    type: 'GET',
    processData: false,
    contentType: 'application/x-www-form-urlencoded;charset=utf-8',
    success: function(data) {
        // 处理返回数据
    },
    error: function(xhr, textStatus, errorThrown) {
        // 处理错误
    }
});

2.3. Encoding and decoding conversion

If other encoding methods must be used, we can manually perform encoding and decoding operations. In jQuery, you can encode and decode strings using the encodeURI and decodeURI methods.

var name = '中文';
var encodedName = encodeURI(name); // 对名称进行编码
$.ajax({
    url: 'xxx?name=' + encodedName, // 将编码后的名称传递给服务器
    type: 'GET',
    success: function(data) {
        // 处理返回数据
    },
    error: function(xhr, textStatus, errorThrown) {
        // 处理错误
    }
});

On the server side, we can use JavaScript's unescape function to restore the decoded parameter values.

String str = request.getParameter("name");
str = URLDecoder.decode(str, "UTF-8");
str = unescape(str);
  1. Summary

The above is the solution to the jQuery request URL parameter garbled problem, including modifying the server-side encoding method, modifying the jQuery encoding method, and performing encoding and decoding conversions . By understanding the encoding method and encoding conversion, we can effectively avoid the problem of garbled Chinese parameters, improve efficiency in actual development, and reduce the probability of errors.

The above is the detailed content of jquery request url parameter garbled problem. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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