Ajax 响应在 10% 的时间内不起作用
我的问题是几乎 90% 的情况下都有效,但 10% 的情况下却不起作用。我什么也没得到。它使用带有 onChange 事件的下拉框。
<script type="text/javascript">
function showCourse(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
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)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getcourse.php?q="+str,true);
xmlhttp.send();
}
</script>
<div>
<label for="department">Department</label>
<select name="departments" onchange="showCourse(this.value)">
<option>Select a department:</option>
<?php
for ($i = 0; $i < sizeof($mySchool->departments); $i++) {
echo "<option value=\"" . $mySchool->departments[$i]->id . "\">" . $mySchool->departments[$i]->title . "</option>\n ";
}
?>
</select>
<span id="departInfo">Choose from the following.</span>
</div>
<div id="txtHint"></div>
最后这是 ajax 在 getcourse.php 中调用的内容
<?php
require_once('auth.php');
$q = $_GET["q"];
$sql = "SELECT * FROM courses WHERE department_id = '" . $q . "'";
$result = mysql_query($sql);
echo "<table border='1' width='600'>
<tr>
<th>Link</th>
<th>Section</th>
<th>Name</th>
<th>Map ID: </th>
</tr>";
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['link'] . "</td>";
echo "<td>" . $row['section'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['map'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
My problem is that works almost 90% of the time, but it doesn't work 10%. I just get nothing. Its using a drop down box with a onChange event.
<script type="text/javascript">
function showCourse(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
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)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getcourse.php?q="+str,true);
xmlhttp.send();
}
</script>
<div>
<label for="department">Department</label>
<select name="departments" onchange="showCourse(this.value)">
<option>Select a department:</option>
<?php
for ($i = 0; $i < sizeof($mySchool->departments); $i++) {
echo "<option value=\"" . $mySchool->departments[$i]->id . "\">" . $mySchool->departments[$i]->title . "</option>\n ";
}
?>
</select>
<span id="departInfo">Choose from the following.</span>
</div>
<div id="txtHint"></div>
Then finally here is what the ajax is calling in getcourse.php
<?php
require_once('auth.php');
$q = $_GET["q"];
$sql = "SELECT * FROM courses WHERE department_id = '" . $q . "'";
$result = mysql_query($sql);
echo "<table border='1' width='600'>
<tr>
<th>Link</th>
<th>Section</th>
<th>Name</th>
<th>Map ID: </th>
</tr>";
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['link'] . "</td>";
echo "<td>" . $row['section'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['map'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MISE 的较新版本(我认为是 8+)正在缓存 AJAX 响应。奇怪,但却是事实。你不能通过设置 no-cache 或类似的标头来欺骗 MSIE,MSIE 更清楚哪些情况应该缓存 AJAX 响应(MS 的人认为:总是) - 所以最好的办法是使用唯一的 URL ,说:
(个人说明:我的 Linux 上安装了 Firefox、Chrome、Opera、MSIE6(!),并且我已经使用所有这些浏览器测试了我的应用程序 - 你可以想象,当用户说,它在他的机器上不起作用,出现了初始屏幕,但没有显示任何进一步的更改,我不得不从我的一位朋友那里借了一台装有 Windows 的笔记本。)
Newer versions (8+, I think) of MISE are caching the AJAX responses. Strange, but true. You can't fool MSIE by setting no-cache or similar headers, MSIE knows better which cases should be AJAX responses cached (guys at MS thinks: always) - so the best to do is use unique URLs, say:
(Personal note: I have Firefox, Chrome, Opera, MSIE6(!) installed on my Linux, and I've tested my app with all of these browsers - you can imagine, it was such a surprise when the user said, it does not works on his machine, the initial screen appears but no further changes displayed. I have had to borrow a notebook with Windows from one of my friend.)