서비스 :
app.factory('UserFactory', function ($resource) {
return $resource('http://localhost:8080/demoApp/service/users', {}, {
show: { method: 'GET', isArray: true },
})
});
컨트롤러 :
function userListController($scope, UserFactory){
$scope.showUser = function() {
alert("Show user ...");
$scope.fetchedData = [];
$scope.users = UserFactory.show();
$scope.fetchedData.push($scope.users);
};
}
에서 CORS를 극복하기 위해 우리는 Java CORS Filter를 다음과 같이 구현했습니다. -
Following line were written in doFilter method inside a Filter -
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
$ http.jsonp와 완벽하게 작동합니다. 위의 방법으로 호출하는 동안 브라우저의 자바 스크립트 콘솔 (Chrome)에서 다음 오류가 발생합니다.
XMLHttpRequest는 http://localhost:8080/demoApp/service/users
을로드 할 수 없습니다. 'Access-Control-Allow-Origin'헤더가 요청 된 리소스에 없습니다. 따라서 Origin http://localhost:63342
은 액세스가 허용되지 않습니다.
제발 도와주세요 ...
감사합니다,
서버에서 CORS 클라이언트 도메인을 허용해야합니다. http://tostring.it/2014/03/04/how-to-use-CORS-with-ASPNET-WebAPI-2/ – bobah75
$ resource accepts using colon 매개 변수. \\ : –
으로 콜론을 이스케이프 처리해야합니다. 클라이언트와 서버가 같은 위치에서 실행되지 않기 때문에 최신 웹 브라우저가 CORS를 구현합니다. 서버 측의 정확한 위치에'response.setHeader ("Access-Control-Allow-Origin", *);을 적용하고 싶습니까? –