Status 403 (Forbidden) for PUT and DELETE using AJAX

I implemented a small REST API using JAX-RS (Jersey 2.0), and I use AJAX to call the API, GET and POST, but when I get a call to any PUT or DELETE methods, all I get is the following error message:

Failed to load resource: server responded with status 403 (Forbidden)

Here is an example of the DELETE method in Java:

@Path("/deleteSomething") @DELETE @Consumes("application/json") public void delete(String json) throws ParseException { JSONParser parser = new JSONParser(); Object obj = parser.parse( json ); JSONObject object=(JSONObject)obj; String id = (String) object.get("id"); System.out.println("ID : " + id); //DO SOMETHING HERE } 

And here is the Javascript call using AJAX:

 function deleteSomethingAjax() { $.ajax({ url: API_URI + "/deleteSomething", //API_URI is the API uri contentType : 'application/json', data: idToJSON(), // this function just returns a JSON obj {"id":"myID"} type: 'DELETE', success : function(data, textStatus, jqXHR) { alert( "Fine!" ); }, error : function(jqXHR, data, textStatus, errorThrown) { alert('WOOPS, something wrent wrong...'); } }); } 

Any help would be greatly appreciated !! Thanks!

+6
source share
1 answer

The problem is related to the CORSFilter setting, I incorrectly specified it in the web.xml file . Here is the CORSFilter part well installed inside web.xml :

 <filter> <filter-name>CorsFilter</filter-name> <filter-class>org.apache.catalina.filters.CorsFilter</filter-class> <init-param> <param-name>cors.allowed.origins</param-name> <param-value>*</param-value> </init-param> <init-param> <param-name>cors.allowed.methods</param-name> <param-value>GET,POST,HEAD,OPTIONS,PUT,DELETE</param-value> </init-param> </filter> <filter-mapping> <filter-name>CorsFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 
+6
source

Source: https://habr.com/ru/post/970775/


All Articles