【发布时间】:2010-12-19 14:31:41
【问题描述】:
我有一个似乎正在生成有效 XML 输出的 PHP 脚本,我试图通过 ajax XMLHttpRequest 调用将其拉入浏览器。
在进行 ajax 调用后,我可以在 firebug 中看到请求成功并且 xml 看起来有效,但是当我尝试将响应放入变量时,我收到一个错误,说响应为空。
这是 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();
非常感谢您的帮助!
【问题讨论】:
标签: php javascript xml ajax