【发布时间】: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