I'm currently grappling with how to execute a DELETE request solely using JavaScript. I have a Java Spring service where the controller for the specific URL employs method = RequestMethod.DELETE
. The URL in question is something along the lines of
http://192.168.50.51/my-service/deleteLocation/{value1}/{value2}/{value3}
. In my JavaScript, I've implemented an AJAX function as follows:
ajaxFunction : function(url, callback, httpMethod) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var jsonParse = JSON.parse(xhttp.responseText);
callback(jsonParse);
}
}
xhttp.open(httpMethod, url, true);
xhttp.send();
}
When attempting to utilize the DELETE
URL, I trigger this method via an event handler attached to a button:
deleteConfirm : function() {
var value1 = this.value1;
var value2 = document.getElementById('element-id').getAttribute('data-element');
var value3 = document.getElementById('element-id').getAttribute('data-element2');
var url = 'http://192.168.50.51/my-service/deleteInfo/' + value1 + '/' + value2 + '/' + value3;
var httpMethod = 'DELETE';
var deleteCallback = function() { alert('deleted!'); }
this.ajaxFunction(url, deleteCallback, httpMethod);
}
However, I consistently encounter an error in my console:
my-javascript.js:59 DELETE http://192.168.50.51/my-service/deleteInfo/123456789/123-456-7AB/12699 406 (Not Acceptable)
.
I've come across information stating that XMLHttpRequest supports only GET
and POST
. How should I approach crafting a delete request through pure JavaScript?