【发布时间】:2011-10-14 01:31:14
【问题描述】:
我从 Internet 获得了一个简单的 AJAX 演示代码,它使用数据库中的值动态填充一个选择列表框。它包含一个嵌入了 AJAX 代码的 html 文件和一个 PHP 文件。
问题是这段代码在 IE、Chrome、FireFox4 中运行良好,但在 FireFox3 中无法运行。请任何人解释这一点并告诉我解决方案。代码如下供参考
city表的数据库架构如下
id tinyint(4) primary key not null
city varchar(50)
countryid tinyint(4)
HTML 文件
<html>
<head>
<script type="text/javascript">
function getCity(strURL)
{
alert("inside getCity function");
//var req = getXMLHTTP(); // function to get xmlhttp object
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
//xmlhttp=new XMLHttpRequest();
req=new XMLHttpRequest();
}
else
{// code for IE6, IE5
//xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
req=new ActiveXObject("Microsoft.XMLHTTP");
}
if (req)
{
req.onreadystatechange = function()
{
if (req.readyState == 4) { //data is retrieved from server
if (req.status == 200) { // which reprents ok status
document.getElementById('citydiv').innerHTML=req.responseText;
}
else
{
alert("There was a problem while using XMLHTTP:\n");
}
}
}
//alert(srtURL);
var sURL="findcity.php?country="+strURL;
req.open("GET", sURL, true); //open url using get method
req.send();
}
}
</script>
</head>
<body>
<form method="post" action="" name="form1">
Country : <select name="country" onChange="getCity(this.value)">
<option value="">Select Country</option>
<option value="1">india</option>
<option value="2">usa</option>
</select>
<br />City : <div id="citydiv">
<select name="select">
<option>Select City</option>
</select>
</div>
</form>
</body>
</html>
PHP 文件
<?
echo $_GET['country'];
echo "<br>";
$country=intval($_GET['country']);
echo $country;
echo "<br>";
$link = mysql_connect('localhost', 'root', 'mark'); //change the configuration if required
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('querytest'); //change this if required
$query="select city from city where countryid=$country";
echo "<br>";
echo $query;
echo "<br>";
$result=mysql_query($query);?>
<select name="city">
<option>Select City</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value><?=$row['city']?></option>
<? } ?>
</select>
请朋友们指导我让这段代码在 FireFox 3 中工作
【问题讨论】:
-
javascript 控制台向您报告什么错误?
-
你能做一些调试吗?什么不起作用,您看到什么错误消息?
-
@J.Steen 它没有给出任何错误消息,但它没有像选择国家一样从数据库中动态填充城市选择框
-
它给出了以下异常“错误:未捕获的异常:[异常...”没有足够的参数 [nsIXMLHttpRequest.send]”nsresult:“0x80570001(NS_ERROR_XPC_NOT_ENOUGH_ARGS)”位置:“JS 框架 :: @987654321 @ :: getCity :: 第 37 行“数据:无]”