i am using angularjs (an old one) and javax.ws.rs 2.1.6
When the angular code trys to make the call to localhost:8088
I get the error
Access to XMLHttpRequest at ‘http://localhost:8088/api/parse/’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.
My angular code looks like (being called from localhost:8080)
$http({
method : 'POST',
url : 'http://localhost:8088/api/parse/',
crossDomain: true,
cache:false,
data : $.param( {
vid: vid,
file: file
}),
headers : { 'Content-Type': 'application/json; charset=UTF-8' }
})
.success(function(data) {
return data;
}).
error(function(data, status, headers, config) {
return data;
});
ON the server side I have http://localhost:8088/api/parse. for now using wild cards to just getting it working..
When i use the debugger, i know that the filter is being called.
@Provider
public class CORSFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
responseContext.getHeaders().add("Access-Control-Allow-Credentials", "true");
responseContext.getHeaders().add("Access-Control-Max-Age", "1209600");
responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
responseContext.getHeaders().add("Access-Control-Allow-Headers", "*");
responseContext.getHeaders().add("Access-Control-Allow-Methods", "*");
}
}
In my app file i have
@ApplicationPath("/api")
public class RestApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
public RestApplication() {
singletons.add(new CORSFilter());
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
And then finally in my resource file i have
@POST
@Consumes("application/json")
@Produces("application/json")
@Path("/parse")
public Response parsePost(String jsonPayload, @Context HttpHeaders headers) {
try {
String json = "{"success":"ok"+}";
return Response.ok(json, MediaType.APPLICATION_JSON).build();
}catch (NotAuthorizedException ex){
return Response.status(Response.Status.UNAUTHORIZED).entity(ex.getMessage()).build();
}catch (Exception ex){
ex.printStackTrace();
return Response.serverError().entity(ex.getMessage()).build();
}finally {
}
}
Thanks for any help or suggestions