Chris
Chris

Reputation: 51

Connection AJAX, CouchDB and JavaScript

i've got a little problem with AJAX, CouchDB and JavaScript.

I can open the following URL from CouchDB in my browser: http://192.168.1.58:5984/mydb/name

new Ajax.Request('http://192.168.1.58:5984/mydb/namee', {
  method: 'POST',
  onComplete: function(transport) {
   alert(transport.responseText);
  }
 });

I always get empty alert.

Can you help me?

Upvotes: 5

Views: 2775

Answers (2)

loomi
loomi

Reputation: 3096

The problem here is, that your browser doesn't allow you to make a query on an other web server than the one where you're script originates. (Google for: Same Origin Policy)

But there is a kind of a common technique which is a workaround for this use case. It's called JSONP. Since version 1.0 you have to activate this functionality first in CouchDB. In the section [httpd] of your CouchDB configuration file (.ini) you have to add an

allow_jsonp = true

After this is done you can produce JSONP queries on your CouchDB. Basically adding dynamically lines like this:

<script type="text/javascript" 
     src="http://server2.example.com/getjson?callback=parseResponse">
</script>

But for details refer to the article linked above.

Anyway I propose on the JavaScript side of things to use a Framework as jQuery, DojoToolKit, ect. In jQuery e.g. it is enough to add "?callback=?" at the end of the URL.

Upvotes: 5

ozk
ozk

Reputation: 2022

AJAX doesn't support cross domain scripting. all calls need to be to a URL with the same domain as the one of the current document. a good solution would be to build a proxy service on the server side, that will take the local request, make an HTTP call to the couchDB server, and return it's response.

Upvotes: 3

Related Questions