【问题标题】:Results from query to SQL database not showing until temporary internet files deleted直到临时 Internet 文件被删除,查询到 SQL 数据库的结果才会显示
【发布时间】:2013-07-26 14:34:31
【问题描述】:

我有两个网页。第一个允许用户更改教师所属的部门。按钮的操作调用php 文件,该文件发送所需的SQL 命令,并将信息正确添加到SQL 数据库中。

第二个然后显示两个下拉框。选择部门后,第二个下拉列表会列出该部门的教师。

为了获取我使用的这些数据:

<script type="text/javascript">
function checkTeacherList(departmentName, schoolName) 
{
var xmlhttp;

if (departmentName=="All")
  {
//  document.getElementById("departmentTeachers").innerHTML="";
    $("#departmentTeachers").hide(0);
    $("#allTeacherList").show(0);
  return;
  }
 else
{ 
    $("#departmentTeachers").show(0);
    $("#allTeacherList").hide(0);

} 
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("departmentTeachers").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","http://localhost/iobserve/php/getTeachers.php?schoolName="+schoolName+"&departmentName="+departmentName,true);
xmlhttp.send();

如果我从第一页将教师添加到部门,则只有在我手动删除 Internet 临时文件时才会显示在第二页。

我意识到旧数据正在被缓存,但我不确定如何强制重新加载以正确显示数据?

【问题讨论】:

  • 您可以在查询字符串的末尾添加一个随机值,例如&amp;t=math.Random(),以使每次调用时查询字符串都是唯一的

标签: php javascript sql


【解决方案1】:

一种方法是在查询字符串中附加一个随机值,例如当前时间

var d = new Date();
xmlhttp.open("GET","http://localhost/iobserve/php/getTeachers.php?schoolName="+schoolName+"&departmentName="+departmentName+"&nocache="+d.getSeconds(),true);
xmlhttp.send();

或设置标题

 xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");

或者在服务器端设置缓存头

 header("Expires: Sat, 1 Jan 2005 00:00:00 GMT");
 header("Last-Modified: ".gmdate( "D, d M Y H:i:s")."GMT");
 header("Cache-Control: no-cache, must-revalidate");
 header("Pragma: no-cache");

Controlling cache

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-02
    • 2015-01-28
    • 1970-01-01
    • 2019-10-14
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多