1

我的 html 文件位于 Nginx 的 html 文件夹下,我尝试通过 Angular 向 Play 应用程序发送 $http 请求。play应用在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 中的 routes 文件中的相关部分是:

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 错误,配置我的 Web 应用程序的正确方法是什么?

提前谢谢

4

2 回答 2

1

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

Play的文档对此有一些信息。

于 2013-06-17T14:04:02.593 回答
0

在 nginx 服务器配置中:

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

从您的 AngularJS 应用程序配置或服务调用中:

$http.defaults.useXDomain = true;
于 2013-06-17T19:45:05.743 回答