javascript - 如何使用 JavaScript/jQuery 将新数据发布到 CouchDB

标签 javascript jquery couchdb

我找到了答案 here到下面的问题。

我需要在 apache 中设置一个反向代理,通过将以下行添加到我的虚拟主机花了大约 2 分钟;
ProxyPass/couchdb/http://dojo:5984/
由于同源策略,您不能跨端口发布数据。我知道这适用于域,但不适用于不同的端口,因此您设置了反向代理。


我想知道如何使用 JavaScript 或 jQuery 将数据发布到 couchDB。

我跟着这个tut并创建了一个数据库,我可以使用 curl 发布和获取数据,一切正常。下面是我使用的 curl 示例。

我也可以使用 jQuery 获取数据,但我不知道如何 POST 到 CouchDB

curl -X GET http://127.0.0.1:5984/mycouchshop/_all_docs .
curl -X POST http://127.0.0.1:5984/mycouchshop/ -d @person.json -H "Content-Type: application/json"

我能够使用 jQuery 获取和显示数据。下面的代码工作正常。

  $.ajax({
    url : 'http://couchdb:5984/mycouchshop/_design/peoples/_view/people',
    type : 'GET',
    dataType : "jsonp",
    success : function(json) {}
  });

但是发布数据会导致 405 Method Not Allowed

  $.ajax({
    url : 'http://couchdb:5984/mycouchshop/',
    data : {"forename": "Bob", "surname": "McHamster", "type": "person"},
    contentType : "application/json", 
    type : 'POST',
    dataType : "json",
    success : function(resp) {}
  });

最佳答案

同源安全策略

我不是 couchapp 专家,但我遇到了同样的问题。问题是您正在进入跨域限制,您的应用程序从一个端口提供服务,而 couchdb 在另一个端口上访问。来自 couchapp.org :

A common question I get from people starting to write Ajax apps using CouchDB, is "when I try to query the CouchDB with jQuery, it doesn't work." Usually it turns out that they have an index.html file on their filesystem, which is attempting to do an Ajax call against the CouchDB server. After I explain to them the same origin security policy, they start to understand this this means CouchDB needs to serve their HTML (rather than loading it in the browser direct from the filesystem).

So, the simplest possible CouchApp is just an HTML file, served directly from CouchDB, that uses Ajax to load and save data from the CouchDB.

使用couchapp

似乎所有应用程序文件都需要使用 couchapp (http://couchapp.org/page/index)“推送”到 couchdb 服务器。我在 Mac 上工作,所以我使用了 Standalone executable .安装 couchapp 的说明是 there

教程

当您了解 couchapp 的工作原理后,您可以使用 this tutorial

后续步骤

我正在努力弄清楚它们...如果您发现任何好的东西,请分享!祝你好运!

编辑: 我刚找到 this tutorial

关于javascript - 如何使用 JavaScript/jQuery 将新数据发布到 CouchDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6843060/

相关文章:

javascript - 我如何在 document.write 中动态更改 iframe 的大小

javascript - 获取选择框的 html 并附加到另一个 div 中

javascript - 为什么下面的原型(prototype)声明不起作用?

javascript - 如何在jquery中获取当前选定元素的标记名

jquery - Fancybox 下载图片

javascript - 寻找更快或更好的方法来使用 jquery 选择 XML 节点的第 n 个子节点

mongodb - 什么时候应该使用 NoSQL 数据库而不是关系数据库?可以在同一个网站上使用两者吗?

python - 使用 couchdb-python 管理 _users 中的用户

javascript - 每个动态生成元素仅单击一次

ios - 如何在 iOS Couchbase Mobile 中使用 HTTPS 复制?