ldapjs使用ajax客户端进行分页

时间:2017-01-11 19:21:24

标签: ajax node.js ldap ldapjs

尽管ldapjs documentation中涉及分页,但我不清楚如何使用ajax客户端在我的应用程序中实现分页。假设我只是简单地调用服务器来搜索组织中所有人的LDAP,最初是第一页有10个条目,如下所示:

$.ajax({
  method: "GET",
  url: "LDAPSearch",
  data: {
    filter: "(ou=People)",
    pageNum: 1,
    entriesPerPage: 10
  }
}).done(function( result ) {
  console.log('result: ', result);
});

我希望服务器发回包含前10个条目的结果以及搜索产生的条目总数,这样我就知道会有多少页面。在服务器上的ldapjs代码中,我希望在pageNum的opts中有一个参数,例如:

var opts = {
  filter: '(objectclass=commonobject)',
  scope: 'sub',
  paged: true,
  pageNum: 1, //This is not a valid option, but how I would expect it to work
  sizeLimit: 10
};
client.search('o=largedir', opts, function(err, res) {
  assert.ifError(err);
  res.on('searchEntry', function(entry) {
    // do per-entry processing
  });
  res.on('page', function(result) {
    console.log('page end');
  });
  res.on('error', function(resErr) {
    assert.ifError(resErr);
  });
  res.on('end', function(result) {
    console.log('done ');
  });
});

1 个答案:

答案 0 :(得分:0)

您将找到所需信息here

  

...并将通过searchEntry事件输出所有结果对象。在操作期间每个结果的末尾,也会发出页面事件......

因此,使用 searchEntry 事件将结果添加到集合中,例如:

res.on('searchEntry', function(entry) {
  resultArray.push(entry.object);
});

...如果达到sizeLimit(页面),请使用页面事件继续下一页。

res.on('page', function(res, cb) {
  if (cb) {
    cb.call();
  } else {
    // search is finished, results in resultArray
  }
});

请记住,LDAP服务器通常会默认或配置限制搜索。 500是常用的默认值。另请参阅http://www.openldap.org/doc/admin24/limits.html

相关问题