I have two AJAX scripts, with xmlhttp requests. But they're *ing with each other, for example AJAX function 2 will display function 1's xmlhttp.response. I was wondering if there's a way to stop this? I already tried adding xmlhttp.close but that didn't work. Thanks in advance!
我有两个带有xmlhttp请求的AJAX脚本。但它们相互冲突,例如AJAX函数2将显示函数1的xmlhttp.response。我想知道是否有办法阻止这种情况?我已经尝试添加xmlhttp.close但是没有用。提前致谢!
<script>var hello = setInterval(function()
{
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("main").innerHTML=xmlhttp.responseText;
xmlhttp.close;
}
}
xmlhttp.open("GET","ajax.php?user=" +username,true);
xmlhttp.send();
}, 1000);
</script>
<script>var anotherone = setInterval(function()
{
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("sub").innerHTML=xmlhttp.responseText;
xmlhttp.close;
}
}
xmlhttp.open("GET","ajax.php?user=" +username +"&do=" +option,true);
xmlhttp.send();
}, 1000);
</script>
1 个解决方案
#1
3
You have one XMLHttpRequest that you keep writing over. In each function start with the line
你有一个XMLHttpRequest,你一直在写。在每个函数中以行开头
var xmlhttp;
And it'll scope it to that function, rather than the page.
它将范围扩展到该功能,而不是页面。
If you need it scoped to the page for some reason, rename one of them, so that you have e.g. both xmlhttpHello
and xmlhttpAnotherone
.
如果由于某种原因需要将其限定为页面范围,请重命名其中一个,以便例如xmlhttpHello和xmlhttpAnotherone。
#1
3
You have one XMLHttpRequest that you keep writing over. In each function start with the line
你有一个XMLHttpRequest,你一直在写。在每个函数中以行开头
var xmlhttp;
And it'll scope it to that function, rather than the page.
它将范围扩展到该功能,而不是页面。
If you need it scoped to the page for some reason, rename one of them, so that you have e.g. both xmlhttpHello
and xmlhttpAnotherone
.
如果由于某种原因需要将其限定为页面范围,请重命名其中一个,以便例如xmlhttpHello和xmlhttpAnotherone。