XMLHTTPはデータアップロードにUnicodeエンコーディングを使用しており、一般的なページではgb2312を使用しているため、ページ表示時に文字化けが発生します。ページを取得する際、XMLHttp は UTF-8 エンコーディングを返すため、表示が文字化けします。
PHP Ajax コードの文字化けに対する解決策の 1 つは、PHP ファイルで宣言を GB2312 として表示することです
header("Content-Type: text/html;charset=GB2312");
そして、サーバ。
以下の通り
$_POST["content"]=iconv("UTF-8","gb2312",$_POST["content"]);
これでPHP Ajaxの文字化け問題は解決できます
方法2 、はい、すべて UTF-8 でエンコードされます。ここで言うことはあまりありません
PHP Ajax コード文字化けの解決例クライアント側
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> !DOCTYPE HTML PUBLIC "-//W3C//<br />DTD HTML 4.01 Transitional//EN"</span><span class="tag">></span><span> </span></span></li> <li> <span class="tag"><</span><span> </span><span class="tag-name">html</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> </span><span class="tag-name">head</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><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> </li> <li class="alt"> <span class="tag"><</span><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> </li> <li> <span class="tag"><</span><span> /head</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> </span><span class="tag-name">body</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><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><span> /div</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><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> </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><span> /script</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><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> </li> <li> <span class="tag"><</span><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> </li> <li class="alt"> <span class="tag"><</span><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><span> !--我用submit时就出错--</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><span> /form</span><span class="tag">></span><span> </span> </li> <li class="alt"> <span class="tag"><</span><span> /body</span><span class="tag">></span><span> </span> </li> <li> <span class="tag"><</span><span> /html</span><span class="tag">></span><span> </span> </li> </ol>
PHP Ajax コード文字化け解決例サーバー側
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php </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></span></li></ol>
上記のコード例は、PHP Ajax コード文字化け I に関連する解決策です。再び助けを必要としている友人たちの助けになれば幸いです。