What to do if PHP ajax transmits Chinese garbled characters

PHPz
Release: 2023-04-19 10:38:09
Original
705 people have browsed it

In the process of developing using PHP combined with AJAX, we may encounter a common problem, that is, garbled characters appear during Chinese transmission. This is because the default encoding method used by PHP is ISO-8859-1, while the default encoding method used by AJAX is UTF-8. The two are not compatible, resulting in garbled characters during the transmission process. This article will introduce how to solve the problem of Chinese garbled characters transmitted by PHP and AJAX.

1. AJAX request sends garbled code

AJAX is a solution for asynchronous interaction between the front end and the back end. Through AJAX, requests can be sent to the back end without refreshing the page, and can Receive data returned by the backend. When using AJAX for Chinese transmission, garbled characters may occur.

For example, the following simple AJAX request:

$.ajax({
    url: "test.php",
    type: "POST",
    data: { name: "张三", age: 20 },
    success: function (result) {
        console.log(result);
    }
});
Copy after login

In the above code, we sent a POST request to test.php and passed a "name" of "Zhang San" , data with "age" as 20. However, when we receive this request parameter on the backend, Chinese garbled characters will appear.

The solution to this problem is very simple, just convert the data to UTF-8 encoding before sending the AJAX request. Modify the above code as follows:

$.ajax({
    url: "test.php",
    type: "POST",
    data: { name: encodeURIComponent("张三"), age: 20 },
    success: function (result) {
        console.log(result);
    }
});
Copy after login

In the above code, we use encodeURIComponent to encode "name" and convert Chinese to UTF-8 encoding, so as to avoid Chinese garbled characters.

2. PHP receives UTF-8 encoded data

In the previous chapter, we introduced how to perform encoding conversion in AJAX requests, so how to receive this UTF-8 encoding in back-end PHP What about the data?

In PHP, we can get the parameters of the POST request through $_POST. However, by default, the encoding method used by PHP is ISO-8859-1, which is incompatible with UTF-8 encoding, so we need to perform corresponding encoding conversion when obtaining data.

Modify the test.php file as follows:

Copy after login

In the above code, we first set the response header to UTF-8 encoding, then obtain the parameters through $_POST, and finally use the iconv function Convert UTF-8 encoded parameters to GBK encoding to avoid Chinese garbled characters.

3. JSON transfers Chinese garbled characters

In actual applications, we usually transfer data in JSON format. When transmitting Chinese via JSON, you also need to pay attention to encoding issues.

For example, the following simple JSON data:

var data = {
    name: "张三",
    age: 20
};
Copy after login

In the above code, we define a JSON data containing the "name" and "age" attributes, where "name" is Chinese "Zhang San".

When passing JSON data to the back-end PHP, the data also needs to be encoded and converted. Modify the above code as follows:

var data = {
    name: encodeURIComponent("张三"),
    age: 20
};
Copy after login

In the above code, we use encodeURIComponent to encode "name" and convert Chinese into UTF-8 encoding, so as to avoid Chinese garbled characters.

In the back-end PHP, corresponding encoding conversion is also required. Modify the test.php file as follows:

Copy after login

In the above code, we first obtain the JSON data through $GLOBALS['HTTP_RAW_POST_DATA'] or file_get_contents('php://input'), and then use json_decode to The JSON data is decoded into an array, and finally the iconv function is used to convert the UTF-8 encoded parameters into GBK encoding, so as to avoid Chinese garbled characters.

4. Summary

When developing using PHP combined with AJAX, you may encounter garbled characters during Chinese transmission. In order to solve this problem, we need to encode the data to make it compatible with PHP's default encoding. When using JSON for data transfer, you also need to pay attention to encoding issues. Through the introduction of this article, I hope it can help readers solve the problem of Chinese garbled characters transmitted by PHP and AJAX.

The above is the detailed content of What to do if PHP ajax transmits Chinese garbled characters. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!