admin管理员组文章数量:1325081
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.
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 "Content-Type: application/json"
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) {}
});
But posting data results in a 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) {}
});
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.
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 "Content-Type: application/json"
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) {}
});
But posting data results in a 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) {}
});
Share
Improve this question
edited May 23, 2017 at 10:29
CommunityBot
11 silver badge
asked Jul 27, 2011 at 10:47
screenm0nkeyscreenm0nkey
18.8k18 gold badges62 silver badges75 bronze badges
2
- 1 Try this http://stackoverflow./questions/6395338/how-do-you-post-data-to-couchdb-both-with-and-without-using-javascript – Bharat Patil Commented Jul 27, 2011 at 11:04
- @cloudlight - I had a quick read of that post before. I will go back and have another read. Thanks – screenm0nkey Commented Jul 27, 2011 at 11:18
2 Answers
Reset to default 3Same origins security policy
I am no couchapp expert, but I ran into the same issue. The problem is that your are getting into cross-domain restrictions, your app being served from one port and the couchdb being accessed on another port. From couchapp:
A mon 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.
Using couchapp
It seems that all application files need to be "pushed" to the couchdb server, using couchapp (http://couchapp/page/index). I am working from a Mac, so I used the Standalone executable. Instructions to install couchapp are there
A Tutorial
When you understand how couchapp works, you can use this tutorial
Next steps
I am trying to figure them out... if you find anything good, please share! Good luck!
EDIT: I just found this tutorial
Another solution to resolve the Cross-Origin Resource Sharing (CORS) issue is by changing some settings on your local CouchDB installation.
Just follow the answer posted on this Question: Couchdb cors problems
本文标签: How do I POST new data to CouchDB using JavaScriptjQueryStack Overflow
版权声明:本文标题:How do I POST new data to CouchDB using JavaScriptjQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742148401a2422896.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论