Since XMLHTTP is using Unicode encoding to upload data, and the General page is gb2312, this results in garbled display of the page. When the page is fetched, XMLHTTP returns the Utf-8 encoding, which results in garbled display.
One of the workarounds for PHP Ajax is to display the declaration as GB2312 in a php file.
Header ("content-type:text/html;charset=gb2312");
The Chinese that is sent to the server is transcoded.
As follows
$_post["Content"]=iconv ("UTF-8", "gb2312", $_post["content"]);
So this can solve the PHP ajax garbled problem
Method Two, are all using UTF-8 encoding. There's not much to say here.
PHP Ajax garbled solution to the client of the sample
- < ! DOCTYPE HTML Public "-//w3c//
DTD HTML 4.01 transitional//en ">
- < html>
- < head>
- < meta http-equiv="Content-type"
content="text/html; charset=gb2312 ">
- < title>ajax Post test title>
- < /head >
- < body>
- < div id="msg">< /div >
- < script language="javascript">
- /**
- * Initialize a XMLHTTP object
- */
- function Initajax ()
- {
- var Ajax = false ;
- try {
- Ajax = New ActiveXObject
("Msxml2.xmlhttp");
- } catch (e) {
- try {
- Ajax = New ActiveXObject
("Microsoft.XMLHTTP");
- } catch (E) {
- Ajax = false ;
- }
- }
- if (!ajax && typeof XMLHttp
request!= ' undefined ') {
- Ajax = New XMLHttpRequest ();
- }
- return Ajax;
- }
- In the form test page, there is a form, a displayed layer
- function SendData ()
- {
- var msg = Document . getElementById ("msg");
- var F = Document . Form1;
- var C = F . Content.value;
- URL to receive data
- var URL = "dispmsg.php" ;
- var Poststr = "Content=" +c;
- var Ajax = Initajax ();
- Ajax.open ("POST", url,true);
- Ajax.setrequestheader ("Content-type",
"application/x-www-form-urlencoded");
- Ajax.send (POSTSTR);
- Ajax.onreadystatechange = function (){
- if (ajax.readystate==4 && ajax.status==200) {
- Alert ("I got Something");
- msg.innerhtml = Ajax . responsetext;
- }
- }
- }
- < /script >
- < form name=' form1 '>
- < input type = "text" Name = ' content ' size=ten>
- < input type="button" value="OK"
onclick = "SendData ()" > < !--I made a mistake when I used the submit-- >
- < /form >
- < /body >
- < /html >
PHP Ajax garbled solution to the server side of the example
- < ? PHP
- Header ("Content-type:text
/html; CharSet = GB2312 ");
- if ($_post[' content ')
- {
- $_post["Content"]=iconv ("
UTF-8 "," gb2312 ", $_post[" content "]);
- Print ("content is". $_post[' contents ');
- }
- Else
- {
- Print ("No content sent");
- }
- ?>
The above code example is the PHP Ajax garbled related solutions, I hope that the need for a friend has helped.
http://www.bkjia.com/PHPjc/446195.html www.bkjia.com true http://www.bkjia.com/PHPjc/446195.html techarticle since XMLHTTP is using Unicode encoding to upload data, and the General page is gb2312, this results in garbled display of the page. The XMLHTTP returned when the page was fetched is the Utf-8 ...