Solving the problem of Ajax Chinese garbled characters in PHP

巴扎黑
Release: 2023-03-03 12:38:02
Original
1587 people have browsed it

Ajax technology has been used in the PHP project I wrote recently, and the user experience has been greatly improved, but at the same time, a new problem has been introduced, that is, the Chinese in the responseText returned by Ajax will mostly be garbled, even if I specify the page encoding as UTF -8.

This is because XMLHttp decodes the responseBody according to UTF-8 encoding when processing the returned responseText. If the data stream sent by the server is indeed UTF-8 encoding, then the Chinese characters will be displayed correctly, but if it is GBK Or the above situation will occur with other encodings.

The solution is to add a header to the data stream returned by the server to indicate the encoding of the sent data stream, so that XMLHttp will not mess up.

header('Content-Type:text/html; charset=GB2312');
Copy after login


The above is the solution to the Ajax Chinese garbled problem in PHP. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
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!