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.
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); } });
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); } });
In the above code, we use encodeURIComponent to encode "name" and convert Chinese to UTF-8 encoding, so as to avoid Chinese garbled characters.
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:
<?php header('Content-Type:text/html;charset=utf-8');//设置响应头为utf-8编码 $name = $_POST["name"]; $name = iconv("utf-8","gbk",$name);//将数据由utf-8编码转为gbk编码 echo $name; ?>
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.
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 };
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 };
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:
<?php $data = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents('php://input');//获取JSON数据 $data = json_decode($data,true);//将JSON数据解码为数组 $name = $data["name"]; $name = iconv("utf-8","gbk",$name);//将数据由utf-8编码转为gbk编码 echo $name; ?>
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.
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!