views:

434

answers:

4

I'm new to CouchDB, so please bear with me.

I have an instance of CouchDB running on a VM. I can access it just fine through the browser via futon or directly at:

http://192.168.62.128:5984/articles/hot_dog

Calling that URL in a browser returns the proper JSON. But, when I try to call that exact same URL via ajax, I get nothing:

var ajaxUrl = 'http://192.168.62.128:5984/articles/hot_dog';
$.getJSON(ajaxUrl, null, function(data) { alert(data); });

Looking at the response header with Firebug shows me that the HTTP response was 200 and the content-length is the right size. Even the Etag matches with what is in CouchDB. But the response itself is empty!

The URL is absolutely right; I've triple checked, and copy/pasted it directly (and besides it wouldn't give a 200 response if it weren't). I'm using jQuery 1.4.2, and CouchDB 0.8

What's going on?

A: 

firstly this is a json question and u have tagged jquery...

if you need jquery then ... look up load() function.

$('#selector').load(ajaxURL); alert($('#selector').html())

Val
What you state is simply wrong. He clearly uses jQuery which can be seen from his use of `$.getJSON`. `load` is also definitely the wrong function for what he wants. So this question is tagged correctly only json is missing as tag
jitter
+5  A: 

Try appending callback=? to the url like this. This will trigger jQuery to issue a jsonp request.

var ajaxUrl = 'http://192.168.62.128:5984/articles/hot_dog?callback=?';

If this doesn't fix it additionally you should append a sample output of the json this url gives in the browser.

jitter
Ok, this combined with upgrading to couch 0.10.1 did the trick. Thank you! I still don't know why this happens though... why doesn't the ajax call work without the ?callback=? suffix?
swilliams
From what you wrote in the question I assume that it isn't a cross-domain request problem (served by a webserver inside (with different port) or outside the vm both would count as cross-domain request and should be blocked by the browser). Adding the `callback` stuff makes it a `jsonp` request and basically this was just a guess, I'm not familiar with CouchDB so no idea why it doesn't work with a plain json request if cross-domain doesn't apply. As you mentioned the "upgrade" I noticed (googled) that jsonp support only came with version 0.10, so the upgrade was the right thing to do
jitter
+1  A: 

Sounds very strongly like you're trying to make a cross-domain AJAX request, which the browser will reject. To get around this, you can use JSONP like the answer above, but this will restrict you to GET requests; you won't be able to add, modify, or delete records.

If you are trying to do cross-domain AJAX calls with CouchDB, I recommend checking out this library:

http://github.com/benvinegar/couchdb-xd

You are absolutely right that it was a cross-domain issue. I ended up using a library called CouchApp to wrap all my code up into attachments in the couchdb, thus no more cross-domain issue. I'll take a peek at your library though too.Since you are 'more' correct than jitter (and he has karma to spare :-p) I marked your response as the answer. Thanks!
swilliams