0

I'm using the Yeoman generator for AngularJS and I would like to interact with a REST server (who uses Spring Boot).

In my computer:

In my AngularJS app I have a service like that :

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

And here is the controller :

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

In my Java app :

@RestController("poc")
public class PocController {

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

But when I want to do the call, I'm being blocked by "CORS"...

What is the best way to do what I want to do ? Not only for GET request, but POST, etc...

Is it possible to "configure" the REST server URL (http://localhost:8080/) once and make all AngularJS call (like /poc/hello) redirected to http://localhost:8080/poc/hello ? If yes, how ?

1
  • 1
    Read this Guide. Commented Mar 12, 2016 at 13:41

1 Answer 1

2

set headers in your rest server, following is some data I set on my node server but the details should help:

'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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.