定期调用XMLHttpRequest.send()的问题? [英] Problem of calling XMLHttpRequest.send() periodically?

查看:83
本文介绍了定期调用XMLHttpRequest.send()的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在定期调用XMLHttpRequest对象的send()时遇到一个非常奇怪的问题. send()将提交到php页面(静默),此页面将获取数据库中的某些数据,回显数据(以纯文本格式),然后删除数据.客户端将接收回显的纯文本,并且应该接收一次,但是,当第二次(及以后)调用send()时,除非我重新加载页面,否则仍接收到已删除的文本.

我认为AJAX的工作方式有问题.这是我的代码的片段:

 函数 getMsg(){
  如果(aCyclePassed){
   aCyclePassed =  false ;
    var  receiver =(XMLHttpRequest)?  XMLHttpRequest(): ActiveXObject("  Microsoft.XMLHttpRequest");
  receiver.open(" "  http://localhost/chatting.php?u = currentUser"功能(){
  如果(receiver.readyState == < 4 && receiver.status == < var  msg = receiver.responseText;
  aCyclePassed =  true ;
   如果(msg!= " ) chatBox.value + = msg + " ;
   }
 };
 receiver.send();
}
 setTimeout("  1000 );
} 


chatting.php除了从客户端获取变量u,从数据库中选择一些文本(消息),回显然后从数据库中删除它以使客户端仅获取并显示一次外,没有什么特别的,但是正如我所说,这似乎在我重新加载客户端页面之前,不要删除(实际上是删除).

你能帮帮我吗?这是我的第一个使用PHP的在线聊天应用程序,非常感谢!但它们并不表示IE,因此您最好使用JQuery来使用它们.


Hi, I''m encountering a very strange problem with calling send() of a XMLHttpRequest object periodically. The send() will submit to a php page (silently), this page will fetch some data in the database, echo the data (in plain text format) and then delete the data. The client side will receive the echoed plain text and should receive it once time, but when the send() is called the second time (and later times), the deleted text is still received, unless I reload the page.

I think there is something wrong with how AJAX works. Here is a snippet of my code:

function getMsg() {
  if(aCyclePassed) {
   aCyclePassed = false;
   var receiver = (XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttpRequest") ;
  receiver.open("GET", "http://localhost/chatting.php?u=currentUser", true) ;
  receiver.onreadystatechange = function() {
  if(receiver.readyState ==4 && receiver.status ==200) {
  var msg = receiver.responseText ;
  aCyclePassed = true;
   if(msg != "") chatBox.value += msg + "\n";
   }
 };
 receiver.send();
}
 setTimeout("getMsg()", 1000);
}


The chatting.php has nothing special than getting the variable u from client, select some text (message) from database, echo it and then delete it from database to make client fetch and display it only one time, but as I said, it seems not to be deleted (in fact it was) until I reload the client page.

Could you help me out please? This is the first online chatting application using PHP of mine, thank you very much!

解决方案

The above given ajax code may work in some of the browsers like mozilla,opera but they not mean IE,so you can better use JQuery to use them.


这篇关于定期调用XMLHttpRequest.send()的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆