Ajax php garbled problem

Source: Internet
Author: User
Tags php file return

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>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.