1

I've been racking my brain for two hours trying to solve an issue when trying to consume a REST DELETE operation implemented with Jersey 2.0 (Java 8) with Jquery Ajax call.

Which is strange is that this is only happening when I call the DELETE method, because I am using GET and POST without issues.

When invoking the ajax call I receive the following message:

XMLHttpRequest cannot load http://127.0.0.1:8080/jersey2rest-sample-1.0/rest/employee/317. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 403.

This is the ajax call code (using react + reddux + jquery):

$.ajax({
        type: 'DELETE',
        url: 'http://127.0.0.1:8080/jersey2rest-sample-1.0/rest/employee/' + id,
        success: function(data) {
            alert('Employee was deleted');  
        },
        error: function(xhr, status, err) {
            alert(err);
        }
})

This is my REST method:

@DELETE
@Produces(MediaType.TEXT_PLAIN)
@Path("{id}")
public boolean removeEmployee(@PathParam("id")int id) {
    return dbLayer.removeEmployee(id);
}

Maybe you would say: "This is an issue with CORS stuff".

But not at all, I have already implemented that in my web.xml file in order to make it work for the GET and POST:

<filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

If you ask me if I have already tried the custom configuration including the allowed methods, allowed headers and so on. Yes I tried, and I got the same error.

I tried using firefox and starting Chrome using the parameters for no web security. But no changes.

I even tried changing my method from DELETE to POST. But for some reason, I'm getting the same.

Any idea why this could be happening? Any suggestion or comment will be nice.

Thanks in advance for your time and help.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51
  • Have you checked in the network log that the header is present? – Adrian Wragg Feb 10 '17 at 22:07
  • Hi, it appears in section: Access-Control-Request-Headers:access-control-allow-origin. What is curious is that I added the "type" property to my ajax request and set it to "jsonp" and now I'm getting something different: jQuery311038443145153905656_1486766290498 was not called – Marcelo Tataje Feb 10 '17 at 22:44

0 Answers0