这个问题是在写ajax时出现的,在不同的浏览器中具体的错误会有不同,但实际上不是这个函数和方法的问题。
var number = xmlDoc.getElementsByTagName("number")[0].childNodes[0].nodeValue;
这个函数本身是没有问题的。后来经过检查有问题的是我的php文件:
<?php$servername ="localhost";
$username = "root";
$password = "123456";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {die("Connection failed: " . mysqli_connect_error());
}
$res=mysqli_select_db($conn,"test");
if ($res==0)
echo "选择数据库出错";//$ttt="'".$_GET['values']."'";
$tt="SELECT * FROM friendship WHERE userId='1'";
$number=0;
//$sql = "SELECT content FROM blog where lookNum=43";
$result1=mysqli_query($conn,$tt);if($result1==0){printf("Error: %s\n", mysqli_error($conn));exit();
}
echo '<?xml version="1.0" encoding="ISO-8859-1"?><person>';
while($row=mysqli_fetch_array($result1))
{echo "<friendld>".$row['friendId']."</friendld>";// echo "<time>".$row['time']."</time>";//echo "<br>";$number++;//echo "<br>";//header("location:url地址");
}
echo "<number>".$number."</number>";
echo '</person>';?>
我单独运行这个php文件时的结果:
发现这结果明显不对,后来对比了一下我原来的代码发现少了关键的两行(在php头部加入):
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
完整的php文件:
<?php//一定要加,因为没加导致错误
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
$servername ="localhost";
$username = "root";
$password = "123456";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {die("Connection failed: " . mysqli_connect_error());
}
$res=mysqli_select_db($conn,"test");
if ($res==0)
echo "选择数据库出错";//$ttt="'".$_GET['values']."'";
$tt="SELECT * FROM friendship WHERE userId='1'";
$number=0;
//$sql = "SELECT content FROM blog where lookNum=43";
$result1=mysqli_query($conn,$tt);if($result1==0){printf("Error: %s\n", mysqli_error($conn));exit();
}
echo '<?xml version="1.0" encoding="ISO-8859-1"?><person>';
while($row=mysqli_fetch_array($result1))
{echo "<friendld>".$row['friendId']."</friendld>";// echo "<time>".$row['time']."</time>";//echo "<br>";$number++;//echo "<br>";//header("location:url地址");
}
echo "<number>".$number."</number>";
echo '</person>';?>
现在的效果:
这样就好了:
这两句的具体意思可参考:https://www.cnblogs.com/dhsx/p/4827926.html