Tomcat 服务器上的 JQuery 长轮询问题
我根据此示例创建了一个 CometServlet http://tomcat.apache.org/ tomcat-7.0-doc/aio.html。然后我尝试使用 JQuery 从中获取数据。代码如下:
$(function() {
$.longPoll = function(url, success, error) {
$.ajax({
url : url,
success: function(data, status) {
$.longPoll(url, success, error);
if (success) {
success(data, status);
}
},
error: function(data, status) {
$.longPoll(url, success, error);
if (error) {
error(data, status);
}
}
});
};
$.longPoll("./comet", "", function(data, status) {
alert("success:" + data);
}, function(data, status) {
alert("error:" + data);
});
});
问题是 success 函数没有触发(尽管我可以在 FireBug 控制台中看到数据来了)。我认为发生这种情况是因为服务器没有关闭响应编写器,但这是长轮询的目标:)
有人知道如何解决它吗?
I created a CometServlet according to this example http://tomcat.apache.org/tomcat-7.0-doc/aio.html. Then I tried to get data from it using JQuery. The code is following:
$(function() {
$.longPoll = function(url, success, error) {
$.ajax({
url : url,
success: function(data, status) {
$.longPoll(url, success, error);
if (success) {
success(data, status);
}
},
error: function(data, status) {
$.longPoll(url, success, error);
if (error) {
error(data, status);
}
}
});
};
$.longPoll("./comet", "", function(data, status) {
alert("success:" + data);
}, function(data, status) {
alert("error:" + data);
});
});
The problem is that the success function doesn't trigger (even though I can see in the FireBug console that the data comes). I think it happens because the server doesn't close a response writer, but it is a goal of the long-polling :)
Does any one have any ideas how it can be solved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要覆盖
xhr onreadystatechange
才能使用 jQuery.ajax()
检查readyState === 3
。例子:You need to overwrite the
xhr onreadystatechange
in order to check forreadyState === 3
with jQuery.ajax()
. Example:问题的解决方案是添加计时器来检查长轮询流中是否有新数据。很好的解释在这里: http:// /www.bennadel.com/blog/1976-Long-Polling-Experiment-With-jQuery-And-ColdFusion.htm
谢谢大家。
The problem solution is to add timer for checking the long-poll stream for a new data. Great explanation is here: http://www.bennadel.com/blog/1976-Long-Polling-Experiment-With-jQuery-And-ColdFusion.htm
Thanks everyone.