如何从angularjs访问couchdb跨域

时间:2012-03-07 21:52:12

标签: couchdb angularjs

我已经设置了一个本地环境,我从json文件加载数据:

var Players = $resource('data/players.json');
var players = Players.query(function(){});

效果很好。

现在我想在不同的域(http://playball.iriscouch.com/players)上访问CouchDB,并根据google buzz示例我尝试了以下内容,但我无法使其工作:

var Players = $resource('http://playball.iriscouch.com/players',  
  {alt: 'json', callback: 'JSON_CALLBACK'},
  {get:     {method: 'JSON'}
});

var players = Players.query(function(){});

我只是收到一个空数组...任何想法?

非常感谢!

淑娜

1 个答案:

答案 0 :(得分:7)

如果您可以控制服务器,请使用CORS

要更正您的示例:

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(){});

请务必检查服务器是否正在响应正确的数据(开发工具栏 - 网络面板)。