2012-03-07 49 views
2

我已經成立了一個當地的環境,在那裏我從JSON文件加載數據CouchDB的跨域:如何訪問從angularjs

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

我只是收到一個空數組...任何想法?

謝謝你!

淑娜

+0

這可能有所幫助:http://kxepal.iriscouch.com/docs/1.3/features/cors.html – Costa 2013-04-13 22:23:31

回答

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

一定要檢查服務器是否正在響應正確的數據(開發人員工具欄 - 網絡面板)。

+0

非常感謝Vojta! – 2012-03-26 13:40:16

+0

所以應該如下所示? {query:{method:'JSONP'} – ganaraj 2012-05-25 10:56:25

+0

@ganaraj謝謝,你是對的。更正爲'JSONP'。 – Vojta 2012-05-27 18:56:07

相關問題