How to access couchdb cross domain from angularjs
Question
I've set up a local environment, where I load data from a json file:
var Players = $resource('data/players.json');
var players = Players.query(function(){});
And it works great.
Now I want to access a CouchDB on a different domain (http://playball.iriscouch.com/players), and based on the google buzz example I've tried the following, but I can't make it work:
var Players = $resource('http://playball.iriscouch.com/players',
{alt: 'json', callback: 'JSON_CALLBACK'},
{get: {method: 'JSON'}
});
var players = Players.query(function(){});
I simply receive an empty array... Any ideas?
Thanks a buch!
Sune
Solution
If you have control over the server, use CORS.
To correct your example:
var Players = $resource('http://playball.iriscouch.com/players',
// These params will be set to each request (?alt=json&callback=JSON_CALLBACK),
// where JSON_CALLBACK placeholder will be replaced with correct callback fn,
// generated by Angular.
{alt: 'json', callback: 'JSON_CALLBACK'},
// You need to configure query method to use JSONP
{query: {method: 'JSONP'}
});
var players = Players.query(function(){});
Be sure to check whether server is responding correct data (Dev Toolbar - Network panel).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow