Heim > Backend-Entwicklung > PHP-Tutorial > 如何正确处理PHP Ajax乱码_PHP教程

如何正确处理PHP Ajax乱码_PHP教程

WBOY
Freigeben: 2016-07-15 13:31:49
Original
828 Leute haben es durchsucht

由于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乱码解决示例之客户端

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> !DOCTYPE HTML PUBLIC "-//W3C//<br>DTD HTML 4.01 Transitional//EN"</span><span class="tag">></span><span>   </span></span></span></li>
<li><span class="tag"><span> </span><span class="tag-name">html</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">head</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span> </span><span class="tag-name">meta</span><span> </span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-Type"<br></span><span> </span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html; charset=gb2312"</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">title</span><span class="tag">></span><span>ajax post test</span><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span> /head</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">body</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span> </span><span class="tag-name">div</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"msg"</span><span class="tag">></span><span class="tag"><span> /div</span><span class="tag">></span><span>   </span></span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">script</span><span> </span><span class="attribute">language</span><span>=</span><span class="attribute-value">"javascript"</span><span class="tag">></span><span>   </span></span></li>
<li><span>/**   </span></li>
<li class="alt"><span>* 初始化一个xmlhttp对象   </span></li>
<li><span>*/   </span></li>
<li class="alt"><span>function InitAjax()   </span></li>
<li><span>{   </span></li>
<li class="alt">
<span> var </span><span class="attribute">ajax</span><span>=</span><span class="attribute-value">false</span><span>;   </span>
</li>
<li><span> try {   </span></li>
<li class="alt">
<span></span><span class="attribute">ajax</span><span> = </span><span class="attribute-value">new</span><span> ActiveXObject<br>("Msxml2.XMLHTTP");   </span>
</li>
<li><span> } catch (e) {   </span></li>
<li class="alt"><span>try {   </span></li>
<li>
<span> </span><span class="attribute">ajax</span><span> = </span><span class="attribute-value">new</span><span> ActiveXObject<br>("Microsoft.XMLHTTP");   </span>
</li>
<li class="alt"><span>} catch (E) {   </span></li>
<li>
<span> </span><span class="attribute">ajax</span><span> = </span><span class="attribute-value">false</span><span>;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li><span> }   </span></li>
<li class="alt"><span> if (!ajax && typeof XMLHttp<br>Request!='undefined') {   </span></li>
<li>
<span></span><span class="attribute">ajax</span><span> = </span><span class="attribute-value">new</span><span> XMLHttpRequest();   </span>
</li>
<li class="alt"><span> }   </span></li>
<li><span> return ajax;   </span></li>
<li class="alt"><span>}   </span></li>
<li><span>//在form 测试页面内有一个表单,一个显示的层   </span></li>
<li class="alt"><span>function sendData()   </span></li>
<li><span>{   </span></li>
<li class="alt">
<span>var </span><span class="attribute">msg</span><span>=</span><span class="attribute-value">document</span><span>.getElementById("msg");   </span>
</li>
<li>
<span>var </span><span class="attribute">f</span><span>=</span><span class="attribute-value">document</span><span>.form1;   </span>
</li>
<li class="alt">
<span>var </span><span class="attribute">c</span><span>=</span><span class="attribute-value">f</span><span>.content.value;   </span>
</li>
<li><span>//接收数据的URL   </span></li>
<li class="alt">
<span>var </span><span class="attribute">url</span><span>=</span><span class="attribute-value">"dispmsg.php"</span><span>;   </span>
</li>
<li>
<span>var </span><span class="attribute">poststr</span><span>=</span><span class="attribute-value">"content="</span><span>+c;   </span>
</li>
<li class="alt">
<span>var </span><span class="attribute">ajax</span><span>=</span><span class="attribute-value">InitAjax</span><span>();   </span>
</li>
<li><span>ajax.open("POST",url,true);   </span></li>
<li class="alt"><span>ajax.setRequestHeader("Content-Type",<br>"application/x-www-form-urlencoded");   </span></li>
<li><span>ajax.send(poststr);   </span></li>
<li class="alt">
<span class="attribute">ajax.onreadystatechange</span><span>=</span><span class="attribute-value">function</span><span>(){   </span>
</li>
<li>
<span>if(</span><span class="attribute">ajax.readyState</span><span>==4 && </span><span class="attribute">ajax.status</span><span>==200){   </span>
</li>
<li class="alt"><span>alert("I got something");   </span></li>
<li>
<span class="attribute">msg.innerHTML</span><span>=</span><span class="attribute-value">ajax</span><span>.responseText;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li><span>}   </span></li>
<li class="alt"><span>}   </span></li>
<li><span class="tag"><span> /script</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span> </span><span class="tag-name">form</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">'form1'</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span> </span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">'content'</span><span> </span><span class="attribute">size</span><span>=</span><span class="attribute-value">10</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"button"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"确定"</span><span> <br></span><span class="attribute">onclick</span><span>=</span><span class="attribute-value">"sendData()"</span><span class="tag">></span><span class="tag"><span> !--我用submit时就出错--</span><span class="tag">></span><span>   </span></span></span></li>
<li><span class="tag"><span> /form</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span> /body</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span> /html</span><span class="tag">></span><span> </span></span></li>
</ol>
Nach dem Login kopieren

PHP Ajax乱码解决示例之服务器端

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php   </span></span></span></li>
<li>
<span>header("Content-Type:text<br>/html;</span><span class="attribute">charset</span><span>=</span><span class="attribute-value">GB2312</span><span>");   </span>
</li>
<li class="alt"><span>if($_POST['content'])   </span></li>
<li><span>{   </span></li>
<li class="alt"><span>$_POST["content"]=iconv("<br>UTF-8","gb2312",$_POST["content"]);   </span></li>
<li><span>print("内容是".$_POST['content']);   </span></li>
<li class="alt"><span>}   </span></li>
<li><span>else   </span></li>
<li class="alt"><span>{   </span></li>
<li><span>print("没有内容发送");   </span></li>
<li class="alt"><span>}   </span></li>
<li>
<span class="tag">?></span><span>  </span>
</li>
</ol>
Nach dem Login kopieren

以上代码示例就是PHP Ajax乱码的相关解决方法,希望对又需要的朋友有所帮助。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446195.htmlTechArticle由于XMLHTTP采用的是Unicode编码上传数据,而一般页面采用的是gb2312,这就造成显示页面时产生乱码。而当在获取页面时的XMLHttp返回的是utf-8编...
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage