首页 文章

AngularJS REST调用另一台服务器

提问于
浏览
0

我正在使用Yeoman generator for AngularJS,我想与REST服务器(使用Spring Boot)进行交互 .

在我的电脑中:

在我的AngularJS应用程序中,我有这样的服务:

angular.module('myApp')
  .factory('User', ['$resource',
    function($resource){
      return $resource('http://localhost:8080/poc/hello', {}, {
        query: {method:'GET'}
      });
  }]);

这是控制器:

angular.module('myApp')
  .controller('RegisterCtrl', ['$scope', 'User', function($scope, User) {
      $scope.hello = User.query();
}]);

在我的Java应用程序中:

@RestController("poc")
public class PocController {

    @RequestMapping("hello")
    public String getHello() {
        return "Hello World !";
    }
}

但是当我想打电话时,我被“CORS”封锁......

做我想做的最好的方法是什么?不仅是GET请求,还有POST等...

是否可以"configure" REST服务器URL(http://localhost:8080/)一次,并使所有AngularJS调用(如 /poc/hello )重定向到http://localhost:8080/poc/hello?如果有,怎么样?

1 回答

  • 2

    在您的休息服务器中设置标头,以下是我在节点服务器上设置的一些数据,但详细信息应该有所帮助:

    'Access-Control-Allow-Origin': 'http://localhost:9000'
    'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, PATCH, DELETE'
    'Access-Control-Allow-Headers': 'X-Requested-With,content-type'
    'Access-Control-Allow-Credentials': false
    

相关问题