如何为PouchDB ajax请求设置自定义标头?

时间:2015-01-15 03:23:38

标签: http-headers authorization pouchdb

我正在使用PouchDB 3.2.1。 我正在尝试为所有ajax请求设置Authorization标头:

db.local = new $window.PouchDB(POUCHDB_NAME);

db.remote = new $window.PouchDB(COUCHDB_URI, {
    skipSetup: true,
    ajax: {
        headers: {
            'Authorization': 'Basic ' + $window.btoa('admin:admin')
        }
    }
});

// Replication
db.local.sync(db.remote, {
    live : true,
    retry: true
});

但它不起作用。请参见屏幕截图:

Screenshot from Chrome dev tools

2 个答案:

答案 0 :(得分:1)

在最新的PouchDB中,这是(click here for the docu)的首选方式:

var db = new PouchDB('http://example.com/dbname', {
  fetch: function (url, opts) {
    opts.headers.set('Authorization', 'token-here');
    opts.headers.set('X-Some-Special-Header', 'foo');
    return PouchDB.fetch(url, opts);
  }
});

答案 1 :(得分:0)

我认为如here所述,您还需要设置:

{skipSetup: true}

在构造函数选项中。

相关问题