如何使用JavaScript/jQuery将新数据发布到CouchDB [英] How do I POST new data to CouchDB using JavaScript/jQuery

查看:81
本文介绍了如何使用JavaScript/jQuery将新数据发布到CouchDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了答案这里是下面的问题.

我需要在apache中设置反向代理,这大约需要2分钟,方法是将以下行添加到我的病毒宿主中;
ProxyPass/couchdb/ http://dojo:5984/
由于具有相同的原始策略,因此无法跨端口发布数据.我知道这适用于域,但不适用于不同的端口,因此您设置了反向代理.

I found the answer here to the question below.

I needed to setup a reverse proxy in apache which took about 2 minutes by adding the following line to my virual host;
ProxyPass /couchdb/ http://dojo:5984/
Because of the same origin policy you can't post data across ports. I knew this applied to domains but not different ports so you set up a reverse proxy.

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

我遵循了这个 tut 并创建了一个数据库,我能够使用curl发布和获取数据,一切正常.下面有一些我使用的curl示例.

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

I would like to know how I can POST data to couchDB using JavaScript or jQuery.

I followed this tut and created a database and I'm able to post and get data using curl and it all works fine. There are curl examples below that I used.

I'm also able to get data using jQuery but I don't know how to POST to 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内容类型:应用程序/json"

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获取并显示数据.下面的代码可以正常工作.

I'm able to get and display data using jQuery. The code below works fine.

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

但是发布数据会导致 405不允许的方法

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

推荐答案

相同起源的安全策略

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

我开始从使用编写Ajax应用程序的人们那里得到一个常见问题 CouchDB,是当我尝试使用jQuery查询CouchDB时,它不会 工作."通常结果是,他们在他们的文档上有一个index.html文件 文件系统,它正在尝试对CouchDB进行Ajax调用 服务器.在我向他们解释了相同的来源安全策略之后,他们 开始了解这一点,这意味着CouchDB需要提供其HTML (而不是直接从文件系统将其加载到浏览器中).

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).

因此,最简单的CouchApp只是一个HTML文件, 直接从CouchDB使用Ajax从 CouchDB.

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

似乎所有应用程序文件都需要使用ouchapp(http://couchapp.org/page/index)推"到该长沙发服务器.我使用的是Mac,因此我使用了独立可执行文件.

了解了沙发应用程序的工作原理后,您可以使用

When you understand how couchapp works, you can use this tutorial

我正在尝试找出它们...如果您发现任何不错的东西,请分享!祝你好运!

I am trying to figure them out... if you find anything good, please share! Good luck!

:我刚刚发现

I just found this tutorial

这篇关于如何使用JavaScript/jQuery将新数据发布到CouchDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆