Home >Backend Development >PHP Problem >How to solve the Chinese garbled problem of php xmlhttprequest

How to solve the Chinese garbled problem of php xmlhttprequest

藏色散人
藏色散人Original
2022-01-12 10:05:372687browse

php Solution to Chinese garbled characters in xmlhttprequest: 1. Unify to UTF-8; 2. When outputting text obtained through XMLHttpRequest, just add text declarations in headers.

How to solve the Chinese garbled problem of php xmlhttprequest

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to solve the Chinese garbled problem of php xmlhttprequest?

Solving the problem of Chinese garbled characters in XMLHttpRequest

The default character encoding for data obtained through XMLHttpRequest is UTF-8. If the front-end page is GB2312 or other encoding, the data obtained is displayed as garbled characters. . Through XMLHTTPRequest, the POST data is also UTF-8 encoded. If the background is GB2312 or other encoding, garbled characters will appear.

Solution:

  • Unify to UTF-8. This is also an inevitable trend of internationalization.

  • When outputting text obtained through XMLHttpRequest, add text declarations in headers (direct HTML declarations have no effect).

For example:

PHP:header('Content-Type:text/html;charset=GB2312');
ASP:Response.Charset = "GB2312"
JSP:response.setHeader("Charset","GB2312");

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to solve the Chinese garbled problem of php xmlhttprequest. 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