【问题标题】:How to stop XMLHTTP from clashing with each other?如何阻止 XMLHTTP 相互冲突?
【发布时间】:2011-12-15 07:38:57
【问题描述】:

我有两个 AJAX 脚本,带有 xmlhttp 请求。 但它们相互冲突,例如 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>

【问题讨论】:

    标签: php javascript html xml ajax


    【解决方案1】:

    你有一个 XMLHttpRequest 需要不断地重写。在每个函数中以行开头

    var xmlhttp;
    

    它会将其范围限定为该函数,而不是页面。

    如果您出于某种原因需要将其限定为页面,请重命名其中一个,以便您拥有例如xmlhttpHelloxmlhttpAnotherone

    【讨论】:

    • 谢谢!完全忘记了这一点,愚蠢的我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    相关资源
    最近更新 更多