I'm trying to call REST service at Java backend from Angular-JS page. I can fetch GET data successfully but could not call @POST resource.
For example at the Java backend here is my RESTful service method -
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public User create(User user) {
User usr1 = new User();
usr1.setId(3);
usr1.setFirstName("John");
usr1.setLastName("Paul");
return usr1;
}
At the AngularJS part here is the Service factory :
var app = angular.module('app', ['ngResource']);
app.factory('UsersFactory', function ($resource) {
return $resource('http://localhost:8080/demoApp/service/users', {}, {
query: { method: 'GET', isArray: true },
create: { method: 'POST'}
})
});
Here is the AngularJS controller :
app.controller('userCreationCtrl', ['$scope', 'UsersFactory', '$location',
function ($scope, UsersFactory, $location) {
$scope.createNewUser = function () {
UsersFactory.create($scope.user);
}
}]);
The error is showing in Javascript Console of the Chrome Browser -
XMLHttpRequest cannot load http://localhost:8080/demoApp/service/users. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access.