Sending a request through Angularjs $http with JSON data to my REST service requires setting headers once the response is returned. The necessary headers are added as follows,
Response.ok()
.entity(emp)
.headers.add("Access-Control-Allow-Origin", "*")
.headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT")
.allow("OPTIONS").build();
However, when sending a post request without data, it works fine.
$http.post('localhost:8000/employer/register')
On the other hand, when data is included in the post request, it fails.
$http({method: 'post', url:serverUrl, data:{name:'abc'}, headers:{'Content-Type':'application/json'} });
This snippet showcases my Rest service.
@Path("/register")
public class EmpService{
@Get
@Path("test")
@Produces
public String test(){
return "works";
}
@Post
@Consumes(MediaType.APPLICATION_JSON)
public Response addEmp(Emp emp){
return Response.ok()
.entity(emp)
.headers.add("Access-Control-Allow-Origin", "*")
.headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT")
.allow("OPTIONS").build();
The browser console displays the following errors:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8081/employer/register (Reason: CORS header 'Access-Control-Allow-Origin' missing).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8081/employer/register (Reason: CORS request failed).
UPDATE: Upon investigation, I discovered that the service is not being invoked as there are no log outputs from Sys.out.println. Can anyone help identify where the issue lies?