有効な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();
ありがとう任意の助けのために非常に多くの!
ありがとう!私は今それを含む...しかし、同じエラーが発生します:xmlDocはnullです...(編集:それは私が最初にxmlDocを使用する行に正確に失敗します:var x = xmlDoc.getElementsByTagName( "place "); –
' header( "Content-type:text/xml");のようなhttpヘッダーを入れるのはどうですか? – ajreal
はい、これがトリックでした!Awesome、thankyou、Ajreal !!! :) –