I\'m trying to create a simple AJAX (via jQuery) request to http://yourusername.couchone.com/ (alsmost the same as if I had installed couchdb on localhost)
Cross-site security model prevents you from doing JSON requests to a different domain.
You need to use JSONP to be able to accomplish that. It does the request as a <script>
include instead of an XMLHTTPRequest. <script>
includes do not have the same security model.
I don't know if couchdb supports JSONP though. Usually the request for JSONP looks like this:
http://someUrl/somePath?jsonp=mycallback
The response data reads that jsonp parameter and returns valid javascript to execute in the parent page's contenxt:
myCallback({ JSON:data, JSON:data });
You have to be sure you trust the JSONP provider, because you're essentially giving them javascript execution access to your page. In your case you probably do since it's your own couchdb database.
There is no other solution, standard $.getJSON() will not work if the passed URL is not the same domain as your page.
P.S. I looked at couchone.com, and I don't see anything which suggests they support JSONP. You're going to need your own server-side wrapper script that simply forwards the request to couchone and sends back the response wholesale (which has the advantage of hiding your actual couchdb provider URL), or else to find a different provider which supports JSONP.
I'd say that MightyE's totally right, up to the postscript -- CouchOne does support JSONP. Go to http://YOURSITE.couchone.com/_utils/config.html and change allow_jsonp
in the httpd
section to true
. After that,
$.ajax({
url: 'http://yoursite.couchone.com/',
type: 'get',
dataType: 'jsonp',
success: function(data) {
alert(data.couchdb);
alert(data.version);
}
});
will work.