Nginx,Play和Angular Access-Control-Allow-Origin错误

时间:2013-06-17 13:01:14

标签: angularjs nginx playframework cross-domain access-control

我的html文件位于Nginx的html文件夹下,我试图通过角度向Play应用程序发送$ http请求。播放应用程序位于端口9210下,而nginx监听端口80(用于查看html文件);我认为这个问题存在于此,因为不同的端口被视为不同的域。

我的Nginx服务器配置是:

listen 80;
        listen 443 default ssl;
        server_name  localhost;
     #ssl on;
     ssl_certificate      /usr/local/nginx/conf/server.crt;
     ssl_certificate_key  /usr/local/nginx/conf/server.key;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

     location / {           
        add_header Access-Control-Allow-Origin http://127.0.0.1;

     }

Play中路线文件的相关部分是:

POST    /processReq             controllers.Application.processReq()

在角度我的$ http请求是:

$scope.url = 'http://ec2-50-112-30-246.us-west-2.compute.amazonaws.com:9210/processReq';
$scope.routeReview = function() {
$http.post($scope.url, { "blanCommand": "read reviews() for=\""+$scope.keywords+"\" mode=full"}).
        success(function(data, status) {
            $scope.status = status;
            $scope.data = data;
        $rootScope.reviews = data;
        alert('success'); 
        $location.path('/review');
        })
        .
        error(function(data, status) {
            $scope.data = data || "Request failed";
            $scope.status = status;   
        alert('fail');
        $location.path('/review');
        });
    };

我的问题如下;根据要求,我收到了Access-Control-Allow-Origin错误,配置我的网络应用程序的正确方法是什么?

提前致谢

2 个答案:

答案 0 :(得分:1)

您需要的是Nginx充当代理,并将请求重定向到Play的特定子路径,同时为其他请求提供HTML文件。

Play的documentation有一些信息。

答案 1 :(得分:0)

在nginx服务器配置中:

location / {           
   add_header Access-Control-Allow-Origin *;
}

从您的AngularJS应用配置或服务电话:

$http.defaults.useXDomain = true;