由于xmlhttp采用的是unicode编码上传数据,而一般页面采用的是gb2312,这就造成显示页面时产生乱码。而当在获取页面时的xmlhttp返回的是utf-8编码,这就造成了显示产生乱码。
php ajax乱码解决方法之一就是在php文件中显示声明为gb2312
header(content-type:text/html;charset=gb2312);
而对于发送到服务器的中文进行转码。
如下
$_post[content]=iconv(utf-8,gb2312,$_post[content]);
因而这样可以解决php ajax乱码问题
方法二,是都采用utf-8编码。这里就不多说了
php ajax乱码解决示例之客户端
!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 testtitle> /head> body> div id=msg> /div> script language=javascript> /** * 初始化一个xmlhttp对象 */ 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; } //在form 测试页面内有一个表单,一个显示的层 function senddata() { var msg=document.getelementbyid(msg); var f=document.form1; var c=f.content.value; //接收数据的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=确定
onclick=senddata()> !--我用submit时就出错--> /form> /body> /html>
php ajax乱码解决示例之服务器端
?php header(content-type:text
/html;charset=gb2312); if($_post['content']) { $_post[content]=iconv(
utf-8,gb2312,$_post[content]); print(内容是.$_post['content']); } else { print(没有内容发送); } ?>
以上代码示例就是php ajax乱码的相关解决方法,希望对又需要的朋友有所帮助。
http://www.bkjia.com/phpjc/446195.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/446195.htmltecharticle由于xmlhttp采用的是unicode编码上传数据,而一般页面采用的是gb2312,这就造成显示页面时产生乱码。而当在获取页面时的xmlhttp返回的是utf-8编...