ajax 只执行1次的问题

cjiyin 2007-11-01 10:40:27
我用AJAX写了一段收邮件的代码,但是每次只执行1次,就是当不关闭页面重新打开时候,无论点多少次,都不执行具体操作,感觉就是把缓存中的读出来一样
代码如下:
//检查邮件函数
function CheckMail(Tag){
var url;
resizediv();
CheckTag=Tag;
document.getElementById("waitID").style.visibility="visible";//显示一个loading层
document.getElementById("status").innerHTML="Checking account..."//显示进度
key=1;
CreateXMLHttpRequest();
url="POPMAIL.ASPX?key=Create&Action="+CheckTag; //读取所有邮箱信息
xmlHttp.open('GET',url,true);
xmlHttp.onreadystatechange=gocallback;
xmlHttp.send(null);
}

function gocallback(){
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
Maxkey= xmlHttp.responseText;
document.getElementById("status").innerHTML="Total " +Maxkey +" Accounts";
setTimeout('pollServer()', 2000);

}
}

}

function pollServer(){//这个函数的作用是根据每个邮箱信息收取邮件
var url;
CreateXMLHttpRequest();
url="POPMAIL.ASPX?key="+key+"&Action="+CheckTag;
xmlHttp.open('GET',url,true);
xmlHttp.onreadystatechange=pollcallback;
xmlHttp.send(null);
}

function pollcallback(){//这个函数的作用是显示当前进度并显示出来
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
document.getElementById("status").innerHTML = xmlHttp.responseText+ " ("+key+"/"+Maxkey+")";
key=parseInt(key)+1;
if (key<=Maxkey) {
setTimeout('pollServer()', 2000);
}
else {
window.Left.location.replace("left.aspx");
document.getElementById("waitID").style.visibility="hidden";
}

}
}
}


具体问题出在哪里?只有第1次执行才调用到POPMAIL.ASPX,和do.aspx2个页面,以后执行都不经过这些页面,到底问题在哪里?

...全文
331 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yjl808 2011-08-29
  • 打赏
  • 举报
回复
ding mrtuzi
wxl9484562 2009-10-20
  • 打赏
  • 举报
回复
r_swordsman 谢谢你的答复 我是新手 找你说的改了一下 果然如此~~~~~
rydanliu 2007-11-10
  • 打赏
  • 举报
回复
应该是页面缓存问题
shagoo 2007-11-09
  • 打赏
  • 举报
回复
楼上写的很详细 顶
r_swordsman 2007-11-09
  • 打赏
  • 举报
回复
对使用 open 的建议(来自JavaScript.System.Net.HttpRequest):

this.Open = this.open = function()
{
if(!this.IsIdle) JavaScript.System.InvalidOperationException.Throw(
"invalid operation, this request is in busy or use, please wait for it to be idle.");



_SetResponseSuccess(false);

this.fireEvent("onopen");

_xmlHttpRequest.onreadystatechange = _onreadystatechange;

// if(_xmlHttpRequest.onreadystatechange != _onreadystatechange)
// {
// JavaScript.System.Exception.Throw("do not change the event handle of XmlHttpRequest, if you want to process this event, use onreadystatechange event of the object that used..");
// }

//
// 关于 IXMLHTTPRequest:
// 当 IXMLHTTPRequest 的 open 方法使用 GET 方式成功打开一个 Web 页面之后,如果该 Web 页面在被缓存在本地,那么当再次
// 使用 IXMLHTTPRequest 的 open 方法打开此 Web 页面时,如果 URL 查询参数相同,即整个 URL 完全相同时,那么在调用
// IXMLHTTPRequest 的 send 方法时,即使是同步执行,在 send 方法中也会触发 onreadystatechange 事件并完成 Web 页面的打
// 开并设置所有其他相关属性为与以前的打开操作一样的值,之后 send 返回并不再会触发 onreadystatechange。
//
// 可能存在以下情况,客户端需要每隔 1 分钟获取服务器的时间一次,客户端执行以下操作:
// xmlHttpRequest.open("GET", "GetTime.aspx", true);
// 这时服务器返回一个值如 2007-05-21 10:23:51,此后多次使用上述语句都将得到和此相同的值,那么如果解决此问题?我们可以
// 改服务器 Web 页面的缓存策略,如在 GetTime.aspx 加入如下代码来使客户端不缓存该 Web 页面:
// Response.Cache.SetNoStore(); // 不缓存
// Response.Cache.SetExpires(DateTime.Now); // 立即过期
// 也可以通过给 Web 页面传递不同的 URL 查询参数,如以下多次的调用:
// xmlHttpRequest.open("GET", "GetTime.aspx?times=1", true); // 第 1 次
// xmlHttpRequest.open("GET", "GetTime.aspx?times=2", true); // 第 2 次
// xmlHttpRequest.open("GET", "GetTime.aspx?times=3", true); // 第 3 次
//
// 注意!如果 IXMLHTTPRequest 的 open 方法使用 POST 方式打开 Web 页面,则无此限制,每次都会获取服务器上最新的版本。
//

if(usepost)
{
try
{
_xmlHttpRequest.open("POST", url, async, _userName, _password);
this.IsIdle = false;
}
catch(ex)
{
var arg = new JavaScript.System.Exception("there is an error while try to open \"" + url + "\".");
arg.innerException = ex;
this.fireEvent("onerror", arg);
return false;
}
_setHttpRequestHeader();
_xmlHttpRequest.send(data);
}
else
{
if(data != null)
{
data = data.toString();
if(data.length > 0) url = url.indexOf("?") != -1 ? url + "&" + data : url + "?" + data;
}
try
{
_xmlHttpRequest.open("GET", url, async, _userName, _password);
this.IsIdle = false;
}
catch(ex)
{
var arg = new JavaScript.System.Exception("there is an error while try to open \"" + url + "\".");
arg.innerException = ex;
this.fireEvent("onerror", arg);
return false;
}
_setHttpRequestHeader();
_xmlHttpRequest.send();
}

this.OpenedUrl = url;

if(async) return true;

_onRequestComplete();

return true;
}
FireElement 2007-11-09
  • 打赏
  • 举报
回复
mrtuzi的是正解
mrtuzi 2007-11-06
  • 打赏
  • 举报
回复
url= "POPMAIL.ASPX?key= "+key+ "&Action= "+CheckTag+"&tm="+new Date().toString();
//tm参数防止缓存用的
非凡笨笨 2007-11-01
  • 打赏
  • 举报
回复
工具——internet选项——设置——每次访问时检查~~~

52,782

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧