2010-12-19 1 views
0

有効なXML出力を生成しているように見えるPHPスクリプトがありますが、これをajax XMLHttpRequest呼び出し。PHPを使用してXMLをエクスポートし、AJAX経由でDOMにプルしますが、xmlは表示されません

ajax呼び出しが行われた後、リクエストが成功し、xmlが有効と表示されますが、応答に変数を入れると、応答がnullであるというエラーが表示されます。ここで

は、PHPのコードです:

<?php 
$q=$_GET["q"]; 

$con = mysql_connect('address.com', 'dbnme', 'password'); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("sql01_5789willgil", $con); 

$sql="SELECT * FROM place"; 

$result = mysql_query($sql); 

$xmlResponse = "<?xml version='1.0' encoding='ISO-8859-1'?>\n"; 
$xmlResponse .= "<placeList>\n"; 

while($row = mysql_fetch_array($result)) 
    { 
    $xmlResponse .= "<place>\n"; 
    $xmlResponse .= "<title>". $row['title'] . "</title>\n"; 
    $xmlResponse .= "<description>" . $row['description'] ."</description>\n"; 
    $xmlResponse .= "<latitude>" . $row['latitude'] ."</latitude>\n"; 
    $xmlResponse .= "<longitude>" . $row['longitude'] ."</longitude>\n"; 
    $xmlResponse .= "<image>" . $row['image'] ."</image>\n"; 
    $xmlResponse .= "</place>\n"; 
    } 

echo $xmlResponse; 

mysql_close($con); 
?> 

、違反のJavaScript:

//Pull in the xml data for the bubbles 
     if (window.XMLHttpRequest) 
      {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
      } 
     else 
      {// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 

     xmlhttp.onreadystatechange=function() 
      { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
      xmlDoc=xmlhttp.responseXML; //pull the xml into the DOM 
      var x = xmlDoc.getElementsByTagName("place"); //return the contents of the xml file (places) to an array called x 
      setupMap(); 
      } 
      } 

     xmlhttp.open("GET","getPlaces.php",true); 
     xmlhttp.send(); 

ありがとう任意の助けのために非常に多くの!

答えて

2

それはあなたが正常に解析DOMを確認するために、ルートノード

// ensure not output before this point 
header("Content-type: text/xml"); // good practice 
while($row = mysql_fetch_array($result)) 
{ 
    ...; 
} 

$xmlResponse .= "</placeList>\n"; 
echo $xmlResponse; 

閉じることを忘れているようだ、応答を示すために、追加のヘッダーはXMLが終了タグと

+0

ありがとう!私は今それを含む...しかし、同じエラーが発生します:xmlDocはnullです...(編集:それは私が最初にxmlDocを使用する行に正確に失敗します:var x = xmlDoc.getElementsByTagName( "place "); –

+0

' header( "Content-type:text/xml");のようなhttpヘッダーを入れるのはどうですか? – ajreal

+0

はい、これがトリックでした!Awesome、thankyou、Ajreal !!! :) –

1

多分、xmlは有効ではありません。 responseTextを読んで結果が得られるかどうかを調べてみてください。また、PHPはxmlを生成するためにDOMオブジェクト自体を持っています。これを使うと、有効なXMLを簡単に作成できます。

[編集]

私の知る限り、あなたは少なくとも出力の最後に</placelist>を忘れてしまいました。

あなたの値をエスケープするのは良い考えです。タイトルの<があなたのXMLを破ります。

+0

良い点は必要かもしれないです。私は今それを含めましたが、同じエラーが発生します、xmlDocはnullです。あなたの他の提案を試みます...ありがとう! –

+0

私はresponseTextを読んでアラートで表示すると動作します - 私は全体の応答を見ることができます。しかし、XMLバージョンはまだNullです...うーん。 –

+0

私はresponseTextをエクスポートして.xmlファイルとして保存し、w3c xmlバリデーターを介してそれを実行しました - それは渡されました - したがって、XMLは間違いなく有効です... –

関連する問題