views:

46

answers:

1

Hello, Experts.

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)

If I go to http://**yourusername**.couchone.com/ via Browser I'll get: {"couchdb":"Welcome","version":"1.0.1"} So, it looks like a serialized JSON.

So I wrote a JS Code:

$(function() {
        $.getJSON('http://www.********.couchone.com/', function(data) {
                console.log(data.couchdb);
                console.log(data.version);

            });
    });

But the code doesn't work. FireBug's console shows that the GET request has no response (the whole line is in red) Everything I can see is a Request-Header and Response-Header, but NO DATA (as response)

Request-Header :

Host :  www.*******.couchone.com
User-Agent :    Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 FirePHP/0.4
Accept :    application/json, text/javascript, */*
Accept-Language :   de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding :   gzip,deflate
Accept-Charset :    ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive :    115
Connection :    keep-alive
Origin :    null

Response-Header :

Server :    CouchDB/1.0.1 (Erlang OTP/R13B)
Date :  Sun, 26 Sep 2010 12:45:47 GMT
Content-Type :  application/json
Content-Length :  40
Cache-Control :  must-revalidate

Ideas? Suggestions?

P.S. Sorry for bad English

+3  A: 

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.

MightyE
I've heard bout CouchApps, they live in a Browser as a single HTML w/ JS and can perform an AJAX request to a CouchDB server. As long as I know the Ajax-talk with couchdb is THE point, couse you don't need any server-side language at all (i.E. PHP)
V-Light