AngularJs——CORS策略已阻止对XMLHttpRequest的访问

AngularJs——CORS策略已阻止对XMLHttpRequest的访问,angularjs,cors,same-origin-policy,Angularjs,Cors,Same Origin Policy,错误: CORS策略已阻止从源“”访问“”处的XMLHttpRequest:请求的资源上不存在“访问控制允许源”标头 我在WebStorm的request.http文件和Postman中测试了get方法,但在我的Angularjs项目中不起作用。 (我从一个Spring boot java项目获得) 我的get函数在postman中返回如下内容 [ { "datetime": "2019-01-10T19:00:00.000+0000", "user": { "firstName":

错误:

CORS策略已阻止从源“”访问“”处的XMLHttpRequest:请求的资源上不存在“访问控制允许源”标头

我在WebStorm的request.http文件和Postman中测试了get方法,但在我的Angularjs项目中不起作用。 (我从一个Spring boot java项目获得)

我的get函数在postman中返回如下内容

[
 {
   "datetime": "2019-01-10T19:00:00.000+0000",
"user": {
  "firstName": "thr",
  "lastName": "an",
  "id": 1
},
"speed": 0.0,
"latitude": 37.421998333333335,
"longitude": -122.08400000000002,
"id": 1
},

{
   "datetime": "2019-01-10T19:01:00.000+0000",
"user": {
  "firstName": "thr",
  "lastName": "an",
  "id": 1
},
"speed": 1.575,
"latitude": 37.42198333333335,
"longitude": -122.080000000002,
"id": 2
}
]
控制台:


添加一个
.catch
块以记录错误:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
    $http.get("http://localhost:8080/api/v1/locations")
      .then(function(response) {
        $scope.result = response.data;
        console.log("OK:", response.data);
    }).catch(function(response) {
        console.log("ERROR:", response);
    });
});

邮递员通常不使用或考虑CORS报头。另一方面,浏览器需要它,如果缺少它,则会出现此错误。 在后端代码中,应该将用于请求数据的域列为白名单。在您的情况下,这将是
http://localhost:8080

我希望这是有意义的。

这是我的控制台是的,这正在工作谢谢,我在我的Java Spring引导控制器中添加了“@CrossOrigin(origins=”,maxAge=3600)”。
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
    $http.get("http://localhost:8080/api/v1/locations")
      .then(function(response) {
        $scope.result = response.data;
        console.log("OK:", response.data);
    }).catch(function(response) {
        console.log("ERROR:", response);
    });
});