阅读来自CouchDB的使用Ajax / jQuery的连续纸CouchDB、Ajax、jQuery

2023-09-10 16:46:37 作者:番茄炒蛋

我要听从CouchDB的使用jQuery的连续变化 - 现在这个工作:

I want to listen for continuous changes from CouchDB using jQuery - now this works:

http://localhost:5984/testdb/_changes?feed=continuous

这意味着每次有一个数据库的更新,我得到JSON的一个新的生产线 - ?但我怎么看使用jQuery关闭此网址更新

which means that I get a new line of json every time there is a db update - but how do I read updates off this URL using jQuery?

我试图用这个,但它不工作:

I tried using this but it doesn't work:

$.ajax(
{
  url : "http://localhost:5984/testdb/_changes?feed=continuous&callback=?",
  dataType : 'json',
  success : function(data)
  {
    alert(data.results.length);
  }
});

修改:$就所谓的成功的功能,并立即返回,它没有民意调查的变化。(在下面的图片阿贾克斯列时间表列是16毫秒)

Edit: $.ajax calls the "success" function and returns immediately, it doesn't "poll" for changes.. (timeline column for ajax column in image below is 16ms)

不,它不是一个跨域Ajax的问题​​ - 我可以在Firebug看到有一个与元素的权数

And no, it isn't a cross-domain ajax problem - I can see in fireBug there is a response with the right number of elements

因此​​,任何指引/建议将AP preciated - 它并不必须是jQuery的 - 普通的老javscript会做以及

So any guidance/advice would be appreciated - it doesn't have to be jQuery - plain old javscript would do as well

推荐答案

关闭我的头顶,我能想到的两个很好的方法可以做到这一点。

Off the top of my head, I can think of two good ways to do this.

(即的setTimeout();

使用计时器,运行AJAX调用上的变化饲料每X秒。您还可以存储您收到的最后一个序列号,这样就可以告诉养活开始的下一次民意调查序列号的变化。这将prevent重复数据,使反应更小。

Using a timer (ie., setTimeout();), run the AJAX call on the changes feed every X seconds. You will also store the last sequence number that you received, so that you can tell the changes feed which sequence number to start at the next time you poll. This will prevent duplicate data and make the responses smaller.

根据什么浏览器,你需要支持,你也许可以使用EventSource的API。这里是一个jQuery实现: https://github.com/rwldrn/jquery.eventsource

Depending on what browsers you need to support, you might be able to use the EventSource API. Here is a jQuery implementation: https://github.com/rwldrn/jquery.eventsource