0

I'm trying to use jquery.couch.js to do couch operations in my ember.js app, but I'm having cors problems, and I have no clue what a good solution is.

It seems to me that couch running on port 5984 would make it basically unusable? Why do requests to different ports cause cors problems? And how on earth do OTHER people end up getting couch to work? I'm immensely confused, and not sure how to proceed.

My couch instance returns this from curl:

{"couchdb":"Welcome","version":"1.2.0"}

The code I'm unsuccessfully trying to run is this:

$.couch.urlPrefix = "http://127.0.0.1:5984";
$.couch.login({
    name: 'name',
    password: 'secret'
});

I've modifed the urlPrefix part several times to things like localhost and removing the http:// for both versions.

The error it's throwing:

XMLHttpRequest cannot load http://127.0.0.1:5984/_session. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

Help me! I humbly recognize my noobiness for saying this, but how is couchdb even useful if this is built right into the basic functionality?

Oh and I'm including jquery.couch.js like this:

<script src="http://localhost:5984/_utils/script/jquery.couch.js"></script>

Using this version of jquery:

jQuery JavaScript Library v1.10.2

and using jquery migrate because of previous issues:

<script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script>

Edit

I just now tried to add crossDomain: true, xhrFields: {withCredentials: true} to my login call, to no avail. Exact same error message. I'm clearly missing a core concept.

blaineh
  • 2,263
  • 3
  • 28
  • 46

2 Answers2

3

The message you are seeing is referring to the server, not the client. Changes made to the client's call will not, as you reported, change the result.

In CouchDB 1.4 specifically, CORS support must be explicitly enabled and an origins declaration must be made. That said, depending on how you are using your CouchDB instance there are two ways to enable it:

  • Change the setting in your local.ini directly and restart your instance, see here for more info: http://wiki.apache.org/couchdb/CORS
  • In the case you have futon available, go to Settings and find the setting there and enable it, in this case no restart is needed.

Update

It seems that the CORS section is not always existent by default, in this case just add it yourself.

Hope it helps.

Community
  • 1
  • 1
intuitivepixel
  • 23,302
  • 3
  • 57
  • 51
  • I'm using version 1.4.0, and futon only has the `credentials` option in the cors section (which is set to `false` as required by the docs), and my local.ini doesn't have a cors section at all. I changed the `origins` option in my default.ini instead, but that had no effect. Any thoughts? – blaineh Oct 15 '13 at 17:47
  • Oh, and I upgraded to 1.4.0 from 1.2.0 because 1.2 didn't support any cors options. – blaineh Oct 15 '13 at 17:50
  • I just caught the line in the docs saying I should add a `[cors]` section to my local.ini, which I've done, and returned everything in default.ini to normal, but futon doesn't seem to be picking up any of these manual changes, and vice versa, so I'm thinking something is just wacky with my couch instance.... – blaineh Oct 15 '13 at 17:59
  • So yours was the right answer, but with a twist. In order to get it to work, I had to scroll to the bottom of the futon configuration page and select `Add a new section` which allowed me to add a field. Now it works! – blaineh Oct 15 '13 at 18:50
  • 1
    @blaineh, ups I'm Sorry, with the couchdb i've worked on this section was already there, but glad you got it working, just added it to my answer for future folks stumbling upon this – intuitivepixel Oct 15 '13 at 18:52
  • Works for me (tested in Chrome and Firefox) with following configuration: [httpd] enable_cors = true [cors] origins = * methods = GET,POST,PUT,DELETE headers = Authorization,Content-Type,Accept – simonox Feb 07 '14 at 08:48
  • Notice for all installing couchdb on debian/ubuntu via package manager. As of today (10-Oct-2014) the version you will get is 1.2 and it does NOT support CORS options. If you want to use them you have to install never version from sources. It took me a bit to realise why my CORS settings did not work, and the reason was I've installed it via apt-get install couchdb and it was version 1.2. – szydan Oct 10 '14 at 09:29
0

For those who are using Cookie authentication (not password authentication) and are reusing the cookie in the Ajax request returned by the CouchDB server, you still need to do this in your $.ajax() requests to CouchDB:

xhrFields: {withCredentials: true},

Which, means you have to open the jquery.couch.js file that you sourced from the couch server and manually insert that option into the javascript.

CORS didn't work for me without both doing this on the client side and setting "credentials=true" on the server side.

The original jquery.couch.js as it is written right now doesn't support the client side sending Cookies with CORS, so you have to do it yourself until someone opens a ticket to get this fixed.

Michael Galaxy
  • 1,213
  • 14
  • 17