错误消息:找不到对象!在AJAX中调用PHP脚本
我创建了一个非常基本的页面,使用AJAX从我的错误消息:找不到对象!在AJAX中调用PHP脚本,php,javascript,ajax,Php,Javascript,Ajax,我创建了一个非常基本的页面,使用AJAX从我的contactMySQL表中检索所有联系人: index.php html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>mycontacts.</title> <script type="text/javascript" src="JavaScripts/PrintContacts.js"></script>
contact
MySQL表中检索所有联系人:
index.php
html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>mycontacts.</title>
<script type="text/javascript" src="JavaScripts/PrintContacts.js"></script>
</head>
<body>
<div class="main-wrapper">
<div id="main-content">
<script type="text/javascript">
printContacts();
</script>
<div id="contacts">
</div>
</div>
</div>
</body>
</html>
<?php
$dbconnection = mysql_connect("localhost", "root", "");
mysql_select_db("mycontacts", $dbconnection);
$command = "SELECT * FROM contact";
$result = mysql_query($command);
echo "<table border='1'>";
// Table headers
echo "<tr><th>Name</th></tr>";
// Print all contacts
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['DisplayName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($dbconnection);
?>
getAllContacts.php
html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>mycontacts.</title>
<script type="text/javascript" src="JavaScripts/PrintContacts.js"></script>
</head>
<body>
<div class="main-wrapper">
<div id="main-content">
<script type="text/javascript">
printContacts();
</script>
<div id="contacts">
</div>
</div>
</div>
</body>
</html>
<?php
$dbconnection = mysql_connect("localhost", "root", "");
mysql_select_db("mycontacts", $dbconnection);
$command = "SELECT * FROM contact";
$result = mysql_query($command);
echo "<table border='1'>";
// Table headers
echo "<tr><th>Name</th></tr>";
// Print all contacts
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['DisplayName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($dbconnection);
?>
我只建议一件事 你能改用完整的网址吗
var url = "http://www.blahblah.com/PHP/getAllContacts.php";
我只建议一件事 你能改用完整的网址吗
var url = "http://www.blahblah.com/PHP/getAllContacts.php";
我猜你的错误就在这里:
var url = "../PHP/getAllContacts.php";
将url替换为绝对url或与域相关的内容,而不是与JS文件相关的内容。我猜您的错误在这里:
var url = "../PHP/getAllContacts.php";
将url替换为绝对url或与域相关的内容,而不是与JS文件相关的内容。使用AJAX时,url应基于浏览器的当前位置而不是javascript文件的位置进行链接 将printContacts()中的url更改为“PHP/getAllContacts.PHP” 编辑:好的。。。我知道了!在PrintContacts.js中,您需要更改此行
url = url + "&sid=" + Math.round(Math.random() * 1000000000);
对此
url = url + "?sid=" + Math.round(Math.random() * 1000000000);
注意这里的问号。让&make查找名为getAllContacts.php&192837的文件,而不是名为getAllContacts.php的文件。使用AJAX时,url应基于浏览器的当前位置而不是javascript文件的位置进行链接 将printContacts()中的url更改为“PHP/getAllContacts.PHP” 编辑:好的。。。我知道了!在PrintContacts.js中,您需要更改此行
url = url + "&sid=" + Math.round(Math.random() * 1000000000);
对此
url = url + "?sid=" + Math.round(Math.random() * 1000000000);
注意这里的问号。让&make查找名为getAllContacts.php&192837的文件,而不是名为getAllContacts.php的文件。我也尝试过,同样的错误。这毫无意义。。是不是我需要在XAMPP(apache)中配置一些东西?我已经尝试过了,同样的错误。这毫无意义。。是不是我需要在XAMPP(apache)中配置一些东西?谢谢你的回复。实际上,我尝试使用绝对url,正如下面@Shakti Singh所推荐的,但错误是相同的。还有其他建议吗?谢谢你的回复。实际上,我尝试使用绝对url,正如下面@Shakti Singh所推荐的,但错误是相同的。还有其他建议吗?谢谢你的提醒。我确实做了更改,但错误是一样的。还有其他建议吗?谢谢。另一个可以尝试的方法是使用Firefox的Firebug扩展,并查看Net选项卡以了解发生了什么。它会告诉您它正在使用的路径、返回代码是什么,等等。这可能会对您有所帮助。好的,我发现添加&SID=是问题的根源。删除适当加载页面的。但我确实需要它来解决缓存问题。你知道如何解决这个问题吗?谢谢,谢谢你的提醒。我确实做了更改,但错误是一样的。还有其他建议吗?谢谢。另一个可以尝试的方法是使用Firefox的Firebug扩展,并查看Net选项卡以了解发生了什么。它会告诉您它正在使用的路径、返回代码是什么,等等。这可能会对您有所帮助。好的,我发现添加&SID=是问题的根源。删除适当加载页面的。但我确实需要它来解决缓存问题。你知道如何解决这个问题吗?谢谢