Pergunta

Configurei um ambiente local, onde carrego dados de um arquivo json:

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

E funciona muito bem.

Agora quero acessar um CouchDB em um domínio diferente (http://playball.iriscouch.com/players) e, com base no exemplo do Google Buzz, tentei o seguinte, mas não consigo fazer funcionar:

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

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

Eu simplesmente recebo um array vazio ...Alguma ideia?

Muito obrigado!

Sol

Foi útil?

Solução

Se você tiver controle sobre o servidor, use CORS.

Para corrigir seu exemplo:

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

Certifique-se de verificar se o servidor está respondendo aos dados corretos (Barra de ferramentas do desenvolvedor - painel Rede).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top