Home  >  Article  >  Backend Development  >  Solution to the problem that php+AJAX will cause garbled characters when transmitting Chinese_PHP Tutorial

Solution to the problem that php+AJAX will cause garbled characters when transmitting Chinese_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:50:03773browse

//If the transmission parameters are given directly, garbled characters will be generated!

Copy code The code is as follows:

http_request.open("POST",url,true);
http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
http_request.send("action=" +strName+"&val="+val); //If the value of val is Chinese, garbled characters will be generated


//The solution is simple: use the escape(string) function in javascript
Copy code The code is as follows:

http_request.open("POST",url,true);
http_request. setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
http_request.send("action="+strName+"&val="+escape(val)); //Value of val Chinese characters will not be garbled

The simplest solution to the Chinese garbled characters in the ResponseText GET returned in AJAX
When using AJAX to GET back a page, the Chinese characters in the RESPONSETEXT will most likely be garbled. , this is because when xmlhttp processes the returned responseText, it encodes the resposeBody into UTF-8 and decodes it. If the server sends a UTF-8 data stream, the Chinese characters will be displayed correctly, and GBK will be sent. It gets messed up when encoding the stream. The solution is to add a HEADER to the sent stream to indicate what encoding stream is sent, so that XMLHTTP will not mess up.

Copy code The code is as follows:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319406.htmlTechArticle//If the transmission parameters are given directly, garbled characters will be generated! Copy the code as follows: http_request.open( "POST",url,true); http_request.setRequestHeader('Content-Type', 'applicatio...
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