Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 332
Chris Avatar asked Aug 02 '10 09:08

Chris


2 Answers

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.

like image 114
loomi Avatar answered Oct 15 '22 13:10

loomi


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.

like image 38
ozk Avatar answered Oct 15 '22 11:10

ozk