duandanxiu6965 2013-07-30 10:50
浏览 29
已采纳

ajax的代码不起作用

first of all, sory for my english...

  • the "func 0" is ajax.
  • the "func 1" is a code of refresh ajax code every 1000 ms. when the code work its refresh the refesh.php page and send the text on it to the "bbb" div.
  • the "func 2" is code that the member write text and then the text send to the send.php page (the text send to the mySQL) and then it show the text in the "aaa" div.

someone can help me understend why the code dont work?

  • if i put in the page just the "func 0" and the "func 1", everything works good.
  • and if i put in the page just the "func 0" and the "func 2", everything also works good.
  • but if i put on the page all the 3 function, its not works. i dont know why whene the member try to send text its send the text to mySQL (func 2), but its show the text from refresh.php (func 1) on the "aaa" div (func 2), instead of showing the text that the member send to the "aaa" div.

i hope you'll be able to figure out what the problem was, I had a little hard to explain


this is the codes:

<!--- func 0 --->
<script>
function refresh(name, url, info, type)
{
var str;
if (type=="send") {
str = document.forms["aaa"]["txt"].value;
}
if (type=="send" && str=="")
  {
  document.getElementById(name).innerHTML="";
  return;
  } 
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(name).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET",url+"?info="+str,true); // send the text to page
xmlhttp.send();  
return false;
}
</script>
<!--- end - func 0 --->


<!--- func 1 --->
<script type="text/javascript">
setInterval("refresh('bbb', 'refresh.php', '', 'refresh')", "1000");
</script>
<div id='bbb'> div to refresh at 1000 ms </div>
<!--- end - func 1 --->


<!--- func 2 --->
<form name='aaa' onsubmit="return refresh('aaa', 'send.php', '', 'send');" method='post'>
txt: <input type='text' name='txt' autofocus='autofocus'> <input type='submit' value=' send '>
</form>
<div id='aaa'> div that <b>*send*</b> txt to sql </div>
<!--- end - func 2 --->
  • 写回答

2条回答 默认 最新

  • douyouyi8878 2013-07-30 10:55
    关注

    The main problem is that xmlhttp is being defined as a global variable, so if you happen to have two AJAX queries loading at the same time, they will interfere with each other. Use var xmlhttp to fix this.

    That being said, you shouldn't support IE6 and even less so IE5. Just do this:

    var xhr = new XMLHttpRequest();
    xhr.open("GET",url+"?info="+str,true);
    xhr.onreadystatechange = function() {
      if( this.readyState == 4 && this.status == 200) {
        document.getElementById(name).innerHTML = this.responseText;
      }
    };
    xhr.send();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度