日期:2014-05-18  浏览次数:20401 次

ajax xmlhttprequest对象只能连接数据库一次吗?
直奔主题:
在TEST2.htm的body onload事件中调JS函数异步从数据库取数据(即若干DIV的ID) 取得后将相应DIV的innerHTML属性设为“是”
假设数据库中存有0001、0002两个ID 第一次打开正常 相应DIV显示为“是” xmlHttp.responseText返回为“0001;0002” 这时候
更改数据库为0001、0002、0004 然后刷新 可xmlHttp.responseText返回仍为“0001;0002” 或者更准确的说 服务器端这时更
本就没运行 页面未变 但若关闭窗口重新打开 则可按照预定要求正常显示 这是为什么呢? 很急 求答案!!!
代码如下:
TEST2.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
  <title>TEST</title>
  <script language="javascript" type="text/javascript" src="TEST2.js">
  </script>
</head>
<body onload="onld()">
<table align="center" border="1" cellpadding="10px">
 <tr>
 <td>
 <div id="0001">否</div>
 </td>
 <td>
 <div id="0002">否</div>
 </td>
 <td>
 <div id="0003">否</div>
 </td>
 <td>
 <div id="0004">否</div>
 </td>
 </tr>
</table>
</body>
</html>
TEST2.js
  // JScript 文件
  var xmlHttp;
  function onld()
  {
  initPage();
  }
  function initPage()
{
  createXMLHttpRequest();
var param = 'TEST.aspx?&uid=' + uid + '&cid=' + classid;
xmlHttp.onreadystatechange = handleInit;
xmlHttp.open('GET',param,true);
xmlHttp.send(null);
}
  function createXMLHttpRequest()
  {
  if (window.ActiveXObject)
  {
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else if (window.XMLHttpRequest)
  {
  xmlHttp = new XMLHttpRequest();
  }
  }
  function handleInit()
  {
  if (xmlHttp.readyState == 4)
  {
  if (xmlHttp.status == 200)
  {
  var hidarr = xmlHttp.responseText;
  if (hidarr != '')
  {
  var arr = hidarr.split(';');
  var len = arr.length - 1;
  for (var i = 0;i < len;i++)
  {
  document.getElementById(arr[i]).innerHTML = '是';
  }
  }
  }
  }
  }
TEST.aspx
代码略 就是在Page_Load事件中查询表WWW中的数据 该表只有一列 存DIV的ID 然后把它们转成字符串用分号隔开返回


------解决方案--------------------
JScript code

function initPage() 
{ 
        createXMLHttpRequest(); 
    var param =  'TEST.aspx?&uid= ' + uid +  '&cid= ' + classid+'&date='+new Date() ; 
    xmlHttp.onreadystatechange = handleInit; 
    xmlHttp.open( 'GET ',param,true); 
    xmlHttp.send(null); 
}

------解决方案--------------------
lz,应该是缓存的关系了,url后面加个随即生成的参数,比如象Math.random(),就可以了
就是var param = 'TEST.aspx?&uid= ' + uid + '&cid= ' + classid + '&ran=' + Math.random();