Ajax 响应在 10% 的时间内不起作用

发布于 2024-11-18 11:46:26 字数 2327 浏览 1 评论 0原文

我的问题是几乎 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗梦空间 2024-11-25 11:46:26

MISE 的较新版本(我认为是 8+)正在缓存 AJAX 响应。奇怪,但却是事实。你不能通过设置 no-cache 或类似的标头来欺骗 MSIE,MSIE 更清楚哪些情况应该缓存 AJAX 响应(MS 的人认为:总是) - 所以最好的办法是使用唯一的 URL ,说:

xmlhttp.open("GET","getcourse.php?q=" + str + "&rnd=" + (+new Date()),true);

(个人说明:我的 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:

xmlhttp.open("GET","getcourse.php?q=" + str + "&rnd=" + (+new Date()),true);

(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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文