Ho implementato una piccola API REST usando JAX-RS (Jersey 2.0) e sto usando AJAX per chiamare API, GET e POST funzionano bene ma quando ottengo per chiamare qualsiasi PUT o DELETE metodi, tutto quello che ottiene è il seguente messaggio di errore:403 Stato (vietato) quando PUT e DELETE utilizzano AJAX
Impossibile caricare risorsa: il server ha risposto con uno stato di 403 (Forbidden)
Ecco un esempio di una DELETE Metodo 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
}
Ed ecco la chiamata JavaScript utilizzando AJAX:
function deleteSomethingAjax() {
$.ajax({
url: API_URI + "/deleteSomething", //API_URI is the API's 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...');
}
});
}
Qualsiasi aiuto sarà molto apprezzato !! Grazie!!
Invece di modificare la domanda per includere la soluzione, sarebbe meglio inviare semplicemente una risposta (e "accettare"). –
@PaulRichter grazie per il suggerimento, questo è quello che ho appena fatto. – charliebrownie
@AntP grazie per il consiglio! Ho appena pubblicato la soluzione qui sotto e l'ho accettata. – charliebrownie