我已经搭建了一个本地的CouchDB数据库,希望能够使用在运行于localhost的网页上的JavaScript将其复制到PouchDB数据库。
使用下面的代码会遇到以下错误:
Origin
http://localhost
is not allowed by Access-Control-Allow-Origin.
如果将REMOTE中的http://
去掉,就不会出现错误,但是没有显示任何被复制的文档。
从Chrome DevTools中查看IndexedDB数据库,可以看到已创建该数据库(但似乎没有文档)。
运行于Chrome 29.0.1535.2 canary。
我能在本地做这件事吗?还是需要搭建远程CouchDB数据库并启用CORS(如CouchDB文档所述)?
var REMOTE = 'http://127.0.0.1:5984/foo';
var LOCAL = 'idb://foo';
Pouch(LOCAL, function(error, pouchdb){
if (error) {
console.log("Error: ", error);
} else {
var db = pouchdb;
Pouch.replicate(REMOTE, LOCAL, function (error, changes) {
if (error) {
console.log('Error: ', error);
}
else {
console.log('Changes: ', changes);
db.allDocs({include_docs: true}, function(error, docs) {
console.log('Rows: ', docs.rows);
});
}});
}
});
httpd> enable_cors = true
后,尝试重新启动Couch? - jchesXMLHttpRequest cannot load http://127.0.0.1:5984/foo/. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
我在重启后检查了Futon:httpd > enable_cors为true。 - Sam Duttoncors > origins
配置部分有什么内容吗?如果有,请确保列表中包括http://localhost
。你可能还想进行一次合理性检查,并确保你正在运行 Couch 1.3.x,因为 1.2 及以下版本没有内置 CORS 支持。 - jchescors > origins
就解决了问题。感谢您的坚持! - Sam Dutton