AngularJS应用程序中的CORS错误

时间:2016-04-22 08:29:10

标签: javascript angularjs https http-headers cors

当我尝试从localhost应用程序发出POST请求时出现以下错误:

XMLHttpRequest cannot load https://www.xxx..yy/json/orders. Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '*, http://localhost:9000', but only one is allowed. Origin 'http://localhost:9000' is therefore not allowed access.

这是我的应用程序结构简介:

ctrl:
.controller('myCtrl', function ($scope,$http) {
      var urlBase = "https://xxx/json/";
      console.log("Hello...");
                 
      $scope.startDirectTransaction = function() {
        console.log("startDirectTransaction form...");
        
       $http({method: 'POST', url: urlBase + 'orders', headers: {
            'api_key': 'xxx'}
        }).then(function(response){ 
              $scope.related = response.data;
              console.log("Success!"); 
            });

      };
app:

<!-- begin snippet: js hide: false -->

1 个答案:

答案 0 :(得分:1)

您正尝试将本地应用中的数据发布到其他域。一般来说,这是违反CORS政策的。

此问题的解决方案是您尝试发布数据的域应该允许通过Access-Control-Allow-Origin

https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

中详细了解CORS