使用 Ajax/jQuery 从 CouchDB 读取连续源

发布于 2024-10-03 03:33:24 字数 689 浏览 0 评论 0原文

我想使用 jQuery 监听 Co​​uchDB 的连续更改 - 现在可以工作了:

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

这意味着每次有数据库更新时我都会得到一个新的 json 行 - 但如何使用 jQuery 读取此 URL 的更新?

我尝试使用这个,但它不起作用:

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

编辑:$ .ajax调用“成功”函数并立即返回,它不会“轮询”更改..(ajax列的时间线列下图中是 16ms)

alt text

不,这不是跨域 ajax 问题 - 我可以在fireBug 有一个包含正确数量元素的响应

所以任何指导/建议将不胜感激 - 它不一定是 jQuery - 普通的旧 javscript 也可以

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

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

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);
  }
});

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)

alt text

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁虫ゝ 2024-10-10 03:33:25

正如 Gjorgji Tashkovski 提到的 feed=continuous 适用于服务器端,您可以使用feed=longpoll 适合您的用例。

(function($) {
  // this is a recursive function
  function longpoll(database, last_seq) {
    var url = "/" + database + "/_changes?feed=longpoll";
    // If we don't have a sequence number, then see where we are up to.
    if (last_seq) {
      url = url + "&since=" + last_seq;
    }
    $.ajax({
      type: "GET",
      url: url,
      dataType: 'json',
      success: function(data) {
        // Now we need to see what to do with the data.
        console.log(document.data.results);

        // And set up the re-run of the fetch query.
        // recursive call
        longpoll(database, data.last_seq);
      }
    })
  }
  
  $.couch.longpoll = longpoll;
}(jQuery));

此示例源代码来自现已存档的博客:
https://web.archive.org/web/20170821130003/http://schinckel.net/2012/01/22/jquery-long-poll-for-couchdb-changes./

As Gjorgji Tashkovski mentioned feed=continuous is intended for server-side, you can use feed=longpoll for your use case.

(function($) {
  // this is a recursive function
  function longpoll(database, last_seq) {
    var url = "/" + database + "/_changes?feed=longpoll";
    // If we don't have a sequence number, then see where we are up to.
    if (last_seq) {
      url = url + "&since=" + last_seq;
    }
    $.ajax({
      type: "GET",
      url: url,
      dataType: 'json',
      success: function(data) {
        // Now we need to see what to do with the data.
        console.log(document.data.results);

        // And set up the re-run of the fetch query.
        // recursive call
        longpoll(database, data.last_seq);
      }
    })
  }
  
  $.couch.longpoll = longpoll;
}(jQuery));

This example source code came from this now archived blog:
https://web.archive.org/web/20170821130003/http://schinckel.net/2012/01/22/jquery-long-poll-for-couchdb-changes./

不再让梦枯萎 2024-10-10 03:33:24

我立即想到了两种好方法来做到这一点。

  1. 使用计时器(即 setTimeout();),每 X 秒对更改源运行 AJAX 调用。您还将存储您收到的最后一个序列号,以便您可以告诉更改源在下次轮询时启动哪个序列号。这将防止重复数据并使响应更小。

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

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

  1. 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.

  2. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文