Because the XMLHTTP uses the Unicode code uploads the data, but the General page uses is gb2312, this causes the display page to produce garbled. And when the page is obtained when the XMLHTTP return is UTF-8 encoding, which caused the display generated garbled.
One solution is to display the declaration as GB2312 in the PHP file
Header ("content-type:text/html;charset=gb2312");
And for the Chinese to send to the server transcoding.
As follows
$_post["Content"]=iconv ("UTF-8", "gb2312", $_post["content"]);
Therefore, this can solve the garbled problem
Method Two, is all using UTF-8 encoding. There's not much to say here.
Attached test routines
Client
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "
<title>ajax post test</title>
<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 xmlhttprequest!= ' undefined ') {
Ajax = new XMLHttpRequest ();
}
return ajax;
}
//There is a form in the form test page, a displayed layer
function SendData ()
{
var Msg=document.getelementbyid ("msg");
var f= Document.form1;
Var c=f.content.value;
//Receive data URL
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=10>
<input type= "button" value= "OK" onclick= "SendData ()" ><!--I made an error with submit-->
</form>
</body>
Server-side
<?php
Header ("content-type:text/html;charset=gb2312");
if ($_post[' content ')]
{
$_post["Content"]=iconv ("UTF-8", "gb2312", $_post["content"]);
Print ("Contents is". $_post[' content ')];
}
Else
{
Print ("No content to send");
}
?>