3

初めて AJAX を試していますが、xml 受信機能に問題があります。responseText を警告すると、サーバーから返された xml を確認できますが、responseXML を取得しようとすると、null とエラーが発生します。

ここに私のxmlを構築するphp関数があります

  header('Content-type: application/xml');
    echo("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    echo("<results>");
    echo("<table><![CDATA[tablereererere]]></table>");
    //echo("<ratedTable>".$_POST['ratedTable']."</ratedTable>\n");
    //echo("<table>".$_POST['table']."</table>\n");
    //echo("<post_id>".$_POST['post_id']."</post_id>\n");
    //echo("<user_id>".$_POST['user_id']."</user_id>\n");
    //echo("<rating>".$_POST['rating']."</rating>\n");
    echo("</results>");

そして、ここに返されたxmlを処理している私のjavascript関数があります

function ajaxReceiver(http_request) {

    //this function continues to run until a result is returned and then it creates the new div
    if(http_request.readyState == 4) {

      response_xml = http_request.responseXML;
      response_text =  http_request.responseText;

      alert(response_text);
      alert(response_xml.getElementsByTagName("table")[0].textContent);
      //document.getElementById('floatingNotification').innerHTML = response_text;
       // alert(http_request.responseXML.getElementsByTagName("table")[0].textContent);
      //ratedTable = responseXML.getElementsByTagName("table").value;
      //alert(ratedTable);
      //message = response.getElementsByTagName('table')[0].textContent;
      //alert(message);
     //alert(message);
//this response contains the xml document that was returned by the php function.You can get any values out of the xml document and 
//use javascript dom to manipulate the contents on the page


    }
}
4

5 に答える 5

2

<xmlcontent-type を正しく設定していても、応答の先頭にタグを付ける必要があることが原因である可能性があります。また、最後のタグを適切に閉じていません。これはうまくいくはずです:

echo("<?xml version='1.0'?>");
echo("<results>");
echo("<ratedTable>".$_POST['ratedTable']."</ratedTable>");
echo("<table>".$_POST['table']."</table>");
echo("<post_id>".$_POST['post_id']."</post_id>");
echo("<user_id>".$_POST['user_id']."</user_id>");
echo("<rating>".$_POST['rating']."</rating>");
echo("<message>$message</message>");
echo("</results>");

XML の定義方法について詳しくは、http ://www.w3.org/TR/REC-xml/#sec-prolog-dtd をご覧ください。

于 2010-11-29T19:42:03.587 に答える
1

親ノードを不適切に ( </results>not <results/>) 閉じているため、(サニタイズした後) すべての POSTDATA を<![CDATA[...]]>タグでラップして安全にする必要があります。これも UTF8 でエンコードされていることを確認してください (「 」を参照utf8_encode()) 。

編集:そして、最初にタグについてwajiwが言っ<?xml version="1.0" encoding="UTF-8" ?>たこと。

編集: CDATA ブロックの使用例

<?xml version="1.0" encoding="UTF-8" ?>
<myNode>
    <myData><![CDATA[
        Now I just throw in my data, for fun and profit!
        This way I can use special, reserved characters like <, > and &!
    ]]></myData>
</myNode>

もう一度編集:

やってみませContent-Type: text/xmlapplication/xmlか?

于 2010-11-29T19:41:32.057 に答える
1

答えは、関数「open()」の非同期プロパティを false に設定することです。このような:

ajaxObject.open("POST", "my_XML_Generator.php", false);
ajaxObject.setRequestHeader("Content-type", "text/xml");
ajaxObject.send();
于 2012-07-08T08:14:32.830 に答える
0

私たちのチームでこのエラーが発生したことがありますが、問題がサーバーから返された XML データにあることに気付くまでに長い時間がかかりました。特に、戻り XML 文字列を生成する PHP スクリプトが原因でした。

私の解決策は、PHP スクリプトの先頭から空白を削除することでした。つまり、スクリプトの先頭にあるスペース、改行、および/またはタブを削除して、スクリプトの最初の部分が<?phpタグ自体になるようにする必要があります。この<?PHPタグは、PHP スクリプトの最初の行の最初のものではないことがわかりました。どういうわけか、2 行目からコードを開始しましたが、最初のスクリプト行は単に空の行でした。

それは私を怒らせ、これを理解するのに少し時間がかかったので、他の誰かがこの解決策から恩恵を受けることを願っています. 他のすべてが失敗した場合に試すのは非常に簡単で簡単です。

于 2014-04-21T22:32:40.477 に答える