4

我有一个 servicestack REST 服务和一个 angular.js 客户端。Web 托管在另一个端口上,然后是 RESTService。

使用 $http 检索数据有效,但使用 $resource 无效。

在 REST 服务中,我设置了以下全局标头:

 GlobalResponseHeaders =
                    {
                    { "Access-Control-Allow-Origin", "*" },
                    { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
                    { "Access-Control-Allow-Headers", "X-Requested-With"}
                    }

现在在 angular.js 中,我可以成功读取 $http 的方法,例如

$http({
        method:'get',
        url:address,
        requesttype:'application/json'
    }).success(function(data) {
        $scope.ServiceStatus = data;
    });

但是用 $ressource 读取相同的地址不起作用我的工厂代码是:

myApp.factory('qaServiceStatus', function($resource){
  return $resource('http://localhost:1338/api/status', {}, {
    query: {method:'GET', headers: {'Content-Type': 'application/json'}, params:{}}

    });
});

我在控制器中以这种方式使用它

$scope.RESTStatus = qaServiceStatus.get({});

我得到的错误是:http://localhost:7000访问控制允许来源不允许来源。
并且 XMLHttpRequest 无法加载http://localhost/api/status。Access-Control-Allow- Originhttp://localhost:7000不允许 Origin。

我究竟做错了什么?

我还尝试将允许的来源更改为确切的地址,但这无济于事

4

1 回答 1

8

这是响应您的客户端的 API,不允许您的客户端发起 CORS 请求。我不知道您使用哪种后端,但您需要查看您的 Access-Control-Allow-Origin 设置以使其正常工作。

更新:

错误或功能,您必须询问 angularjs 团队。Atm 他们将 :port 解释为变量而不是端口。解决方案是逃避:

例子:

$resource('http://localhost:1338\:1338/api/status', {}, {.....
于 2013-09-12T08:29:51.893 回答