【问题标题】:Jquery ajax long polling error- ChromeJquery ajax 长轮询错误-Chrome
【发布时间】:2014-02-15 09:06:30
【问题描述】:

我只是在为一个简单的聊天应用程序尝试 ajax 长轮询。这是js代码。它在 Firefox 中运行良好,但是当涉及到 chrome 时,附加的文本会重复。我无法弄清楚错误是什么。

$(document).ready(function(){
 $('#chatText').keydown(function(event) 
 {  
    if (event.keyCode == 13) {
        $.ajax({
             type: "GET",
             url: "http://example.com/Private.pl",
             data: $('form#chatText').serialize(),
             success: function(data) // same data posted by client to server
            {
            $('#chatText')[0].reset();
            $("#chatLog").append(data);
            $("#chatLog").scrollTop(999999) 
            poll();
            }
      })
    event.preventDefault();
    }         
 });
});

function poll() {
$.ajax({
             type: 'GET',
             url: 'http://example.com/Private.pl', 
             data: $('form#chatText2').serialize(),
             success: function(msg) // other users message from server
            {
            $('#chatLog').append(msg);
            $('#chatLog').scrollTop(999999);
            }, 
        complete: poll,
        timeout: 500000
    });
 }

这里是 HTML sn-p

<div id='chatLog' class='text-Area'></div>
<textarea name='message'></textarea>
<form id='chatText' method='post'>
<input type='submit' hidden>
</form>
<form id='chatText2' method='post'>
<input type='hidden' name='client' value='0'>   
<input type='submit' hidden>
</form>

【问题讨论】:

  • 你也可以发布相关的html吗?
  • 嗨 anurupr 我已经发布了 html sn-p。
  • 你能发布两个电话的msg 的值吗?
  • 抱歉重复,不是错误..我将该消息更改为数据..但是相同的错误仍然存​​在于 chrome 中,但不在 firefox 中..
  • 您可以尝试在轮询时设置短超时。例如。 setTimeout(poll, 1000) 在您调用 poll 函数的两个地方。

标签: javascript ajax long-polling


【解决方案1】:

我找到了。在 Firefox 中,每当我输入新文本并提交时,以前的轮询请求都会中止并开始新的轮询。但是在 chrome 中,每当我输入一条新消息时,都会在旧的挂起请求中添加一个新的轮询请求。因此服务器同时使用相同的消息响应每个轮询请求。每次我发送新消息时都会调用 poll() 方法..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 2011-04-20
    • 2014-05-27
    相关资源
    最近更新 更多