Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Ajax/jQuery从CouchDB读取连续提要_Jquery_Ajax_Couchdb_Jsonp - Fatal编程技术网

使用Ajax/jQuery从CouchDB读取连续提要

使用Ajax/jQuery从CouchDB读取连续提要,jquery,ajax,couchdb,jsonp,Jquery,Ajax,Couchdb,Jsonp,我想使用jQuery来监听CouchDB的连续更改—现在可以了: http://localhost:5984/testdb/_changes?feed=continuous 这意味着每次有db更新时,我都会得到一行新的json——但是我如何使用jQuery读取这个URL的更新呢 我试过使用这个,但不起作用: $.ajax( { url : "http://localhost:5984/testdb/_changes?feed=continuous&callback=?", da

我想使用jQuery来监听CouchDB的连续更改—现在可以了:

http://localhost:5984/testdb/_changes?feed=continuous
这意味着每次有db更新时,我都会得到一行新的json——但是我如何使用jQuery读取这个URL的更新呢

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

$.ajax(
{
  url : "http://localhost:5984/testdb/_changes?feed=continuous&callback=?",
  dataType : 'json',
  success : function(data)
  {
    alert(data.results.length);
  }
});
编辑:$。ajax调用“success”函数并立即返回,它不会“轮询”更改。。(下图中ajax列的时间轴列为16ms)

不,这不是一个跨域ajax问题——我可以在fireBug中看到一个元素数量正确的响应


因此,任何指导/建议都将不胜感激-不必是jQuery-简单的旧javscript也可以

在我的脑海中,我可以想出两种很好的方法来做到这一点

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

  • 根据需要支持的浏览器,您可能可以使用EventSourceAPI。下面是一个jQuery实现:


  • 如前所述,
    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));
    
    此示例源代码来自此现已存档的博客:

    谢谢山姆-我正在做1。马上但是我想使用连续提要方法-我不能使用2,我需要支持尽可能多的浏览器。因为HTTP是无状态协议,所以这通常是“不可能”的。但是,请看一下执行此操作的方法。你现在所做的就是这些方法中的一种。是的,Comet是正确的-谢谢你指出它-如果我能成功地编写代码来处理这个问题,我会把它发布在这里。sami,很高兴我能帮上忙。干杯。
    feed=continuous
    用于服务器端。在浏览器中,使用
    feed=longpoll