如何从相互冲突停止XMLHTTP?冲突、XMLHTTP

2023-09-10 17:55:45 作者:风染叶子卿歌

我有两个AJAX脚本,用XMLHTTP请求。 但他们发生冲突的对方,例如AJAX功能2显示功能1的xmlhttp.response。我想知道是否有办法阻止吗?我已经尝试添加xmlhttp.close,但没有奏效。 在此先感谢!

 <脚本> VAR你好=的setInterval(函数()
{
如果(window.XMLHtt prequest)
  {// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
  XMLHTTP =新XMLHtt prequest();
  }
其他
  {// code对IE6,IE5
  XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
  }
xmlhttp.onreadystatechange =功能()
  {
  如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200)
    {
    的document.getElementById(主)的innerHTML = xmlhttp.responseText。
    xmlhttp.close;
    }
  }
xmlhttp.open(GET,?ajax.php用户=+用户名,真实);
xmlhttp.send();
},1000);
< / SCRIPT>

 <脚本> VAR anotherone =的setInterval(函数()
{
如果(window.XMLHtt prequest)
  {// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
  XMLHTTP =新XMLHtt prequest();
  }
其他
  {// code对IE6,IE5
  XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
  }
xmlhttp.onreadystatechange =功能()
  {
  如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200)
    {
    的document.getElementById(子)的innerHTML = xmlhttp.responseText。
    xmlhttp.close;
    }
  }
xmlhttp.open(GET,?ajax.php用户=+用户名+&放大器;做=+选项,真正的);
xmlhttp.send();
},1000);
< / SCRIPT>
 

解决方案

您有一个,你把写了XMLHtt prequest。在每个函数开始线

  VAR XMLHTTP;
 
医学发展将从疾病 转向养生治未病

,它就会范围,它的功能,而不是网页。

如果你需要它作用于因某种原因页,重命名其中的一个,让你如无论 xmlhttpHello xmlhttpAnotherone

I have two AJAX scripts, with xmlhttp requests. But they're clashing 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!

<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>

解决方案

You have one XMLHttpRequest that you keep writing over. In each function start with the line

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.