ajax triggers,AJAX triggers ReadyState 4 several times in IE

在 IE8 浏览器中,用户遇到一个特定问题:某些 AJAX 请求的 readyState 会触发多次,导致行为不一致。问题只出现在少数特定链接上,而其他 AJAX 链接则正常工作。问题发生时,用户已在 AJAX UI 中进行过导航,点击了多个 AJAX 链接,但每个请求都使用同一个全局变量 xmlHttp。怀疑可能是 IE8 在每次调用 ajaxPost 函数时没有正确替换 onreadystatechange 处理器导致的。其他浏览器如 Safari、Firefox 和 Chrome 没有此问题。解决方案可能涉及清理或适配 IE8 的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am experiencing a strange problem where IE8 will fire readyState 4 three or four times on an ajax request. This happens often but not always, and only for a couple of specific links - the rest of my ajax links are fine. My object creation and onreadystatechange handler are basic, ie.

function ajaxPost(params) {

try {

// Firefox, Opera 8.0+, Safari

xmlHttp = new XMLHttpRequest();

} catch (e) {

// Internet Explorer

try {

xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");

} catch (e) {

try {

xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

} catch (e) {

alert('Unsupported Browser');

}

}

}

xmlHttp.onreadystatechange = function() {

if (xmlHttp.readyState == 4) {

// Do something

}

}

xmlHttp.open("POST", path_to_script);

xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

xmlHttp.setRequestHeader("Cache-Control", "must-revalidate");

xmlHttp.send(params);

}

Not sure if this is relevant - but at the points where the problem happens the user has already been navigating an ajax UI. They have clicked several other ajax links without reloading the page - each link uses a new xmlHttpRequest object with the same global variable name (xmlHttp), but each ajax request finishes before another can be issued. Do I need to do any "cleanup" or something for IE8? This has never happened using Safari, Firefox, Chrome, or any other browser.

Any insight would be much appreciated.

Thanks, Brian

Since your xmlHttp object is global, may be IE8 assigns a new onreadystatechange each time you call ajaxPost instead of replacing it.

That may explain an erratic behavior, as it depends on the number of calls to ajaxPost.

more

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值