Getting an empty response when calling CouchDB over ajax
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 brow开发者_运维知识库ser 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?
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.
As you try to get the data from an other web server as your html file comes you need to do a JSONP query. First of all in the latest version of CouchDB you need to enable JSONP querys in the configuration (.ini) file. (It is disable by default since CouchDB 1.0) In the section [httpd] you need to add an
allow_jsonp = true
After this is done you can produce JSONP queries on your CouchDB.
In jQuery then you have to add ?callback=? to the URL to trigger a JSONP query.
Happy Cross-Origin Resource Sharing everyone.
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
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())
精彩评论