Ajax garbled out of the reason now
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
<! 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" >
/**
* Initialization of 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;
}
In the form test page there is a form, a display 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=10>
<input type= "button" value= "OK" onclick= "SendData ()" ><!--I made an error with submit-->
</form>
</body>