Home  >  Article  >  Backend Development  >  Halo theme mjolnir mix php+AJAX solution to the problem that sending Chinese will cause garbled characters

Halo theme mjolnir mix php+AJAX solution to the problem that sending Chinese will cause garbled characters

WBOY
WBOYOriginal
2016-07-29 08:38:521378browse

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

Copy the 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 very simple: use the escape(string) function in javascript

Copy the 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)); //The value of val is Chinese and will not produce garbled characters


The simplest solution to the garbled Chinese characters in the ResponseText GET returned in AJAX
When using AJAX to GET back a page, most of the Chinese characters in the RESPONSETEXT will appear garbled. This is because when xmlhttp processes the returned responseText, The resposeBody is encoded in UTF-8 and decoded. If the server sends a UTF-8 data stream, the Chinese characters will be displayed correctly, but when the GBK encoded stream is sent, it will be messed up. 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");


The above introduces the solution to the problem that halo theme mjolnir mix php+AJAX will cause garbled characters when transmitting Chinese, including the content of halo theme mjolnir mix. I hope it will be helpful to friends who are interested in PHP tutorials.

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