Angularjs에서 CouchDB Cross Domain에 액세스하는 방법
문제
JSON 파일에서 데이터를로드하는 로컬 환경을 설정했습니다.
var Players = $resource('data/players.json');
var players = Players.query(function(){});
.
그리고 그것은 훌륭하게 작동합니다.
이제는 다른 도메인 (http://playball.iriscouch.com/players)에서 couchdb에 액세스하고 Google 버즈 예제를 기반으로 다음을 시도했지만 일을 할 수 없습니다.:
var Players = $resource('http://playball.iriscouch.com/players',
{alt: 'json', callback: 'JSON_CALLBACK'},
{get: {method: 'JSON'}
});
var players = Players.query(function(){});
.
나는 단순히 빈 배열을받습니다 ... 어떤 아이디어?
덕분에 Buch!
Sune
해결책
서버를 제어하는 경우 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(){});
서버가 올바른 데이터에 응답하는지 확인하십시오 (DEV 도구 모음 - 네트워크 패널).