Steps for sending a DELETE HTTP request using Vanilla JavaScript (without jQuery)

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?

Answer №1

Based on the information provided, it seems that your browser is indeed sending a DELETE request, as indicated by the server's response of 406 (Not Acceptable). This suggests that the server received and acknowledged your DELETE request but decided not to process it. To solve this issue, review the server's API to identify why it returns an HTTP406 error and what modifications are needed in your request for it to be successful.

One effective method for troubleshooting such scenarios is utilizing your browser's developer tools. These tools typically include a section displaying the HTTP requests and responses exchanged between your browser and the server. By examining this data, you can easily pinpoint any errors or discrepancies, aiding you in resolving similar issues in the future.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjust the classes of the static navigation bar based on the background of the section it is positioned over

In the process of developing a website using Bootstrap 4, I encountered a challenge with sections featuring both light and dark backgrounds along with a fixed navbar. The navbar, which is set to dark using the css class bg-dark, becomes indistinguishable ...

Is there a way to change the text (price) when I select an option?

<div class="single-pro-details"> <!--Customize in the CSS--> <h6>Home / Beats</h6> <h4>Unique Lil Tecca Type Beat - New Love</h4> <h2 id="price">$ ...

Attach a click event listener to content loaded through AJAX using only JavaScript, without relying on jQuery

I'm curious about how to attach a click event to an element that will be added later through an ajax call. I know jQuery can handle this like so: $(document).on('click', '.ajax-loaded-element' function(){}); However, I'm not ...

Is it possible to conduct a feature test to verify the limitations of HTML5 audio on

Is there a way to detect if volume control of HTML5 audio elements is possible on iOS devices? I want to remove UI elements related to the volume if it's not alterable. After referring to: http://developer.apple.com/library/safari/#documentation/Aud ...

Begin mastering the art of Asynchronous programming in ASP.NET

I'm not looking for advice on learning specific programming languages and technologies like JavaScript, Ajax, and JQuery. I just need guidance on the best choice for enhancing the desktop experience of my web application. To make it easier for you to ...

ways to undo modifications made to a value within an input field using jquery or javascript

$(document).ready(function () { //Highlight row when selected. $(function () { $('#Cases tr').click(function () { $('#Cases tr').removeClass('selectedRow'); $(this).addClass(&apos ...

Sending a jQuery post using Ajax to a PHP validation script

Exploring new tricks and techniques, I am not using the form validation plugin with jQuery UI, but taking a different approach. A jQuery script posts a form to a PHP script via Ajax to validate the input and send a JSON-encoded string back. Based on the st ...

Getting a vector layer from JSON in OpenLayers 3 can be achieved by following these steps

Below is the script where I have included vector layers vectorLayer, vectorLayer5, vectorLayer1 How can I retrieve that layer from JSON.. I want to add multiple layers from an external JSON file with the icon PNG image // Updated on input change var ra ...

Even though a Jquery ajax error handler function is present, an exception is still being thrown

After submitting a form using ajax, the validation is carried out on the server side. If the validation fails, a json encoded object with the errors and status code 500 is returned. When jQuery receives this response on the client side, the error handler f ...

Turning $.post into $.ajax: A step-by-step guide

I need help with converting my $.post code to $.ajax. Here is the code snippet: $.post("../admin-login", { dataName:JSON.stringify({ username:uname, password:pass, }) }, function(data,status){ console.log("Data:"+data); answer = data; ...

JavaScript for switching between grid layouts

I have organized 3 DIVs using a grid layout. There is a Navigation bar with an on-click event attached to it. When a button on the nav-bar is clicked, I want the JavaScript function to display the corresponding grid associated with that button. Currently, ...

Uncontrolled discord bot flooding with messages despite being set to send messages only once every 60 seconds

const Discord = require('discord.js'); const { Client, MessageAttachment } = require('discord.js'); const client = new Discord.Client(); client.once('ready', () => { console.log("Ready!") }) client.on(&apos ...

SCRAM-SERVER-FIRST-MESSAGE: The client's password is required to be in string format

After researching documentation from various sources on a similar issue, I have not been successful in resolving this specific error within my code. throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string') ^ ...

What advantages does incorporating "function() 'use strict'" into each individual file provide?

As I dive into revamping an older AngularJS 1.3 project, one striking observation is the consistent pattern of starting each AngularJS code file with: (function () { 'use strict'; angular.module('app').factory('Employees', ...

"Encountering an issue with Express.json where it fails to parse the

Receiving JSON POST data from an IoT API that includes multipart form-data. Occasionally, an image file may be included but I only want to focus on the JSON part: POST { host: '192.168.78.243:3000', accept: '*/*', 'content-le ...

Issues with comparing dates

I'm attempting to compare two different dates: Start Date Thursday, October 29th, 2015 at 6:00 PM GMT End Date Friday, October 30th, 2015 at 12:00 AM GMT Simply put, if(end > start) { alert('It works'); } else { alert(&apo ...

Incorporating a PHP file containing both PHP and JavaScript variables into an AJAX document

I'm facing an issue with my application that involves a language file called "lang.php", along with "index.php" and "ajax.php" files. The "lang.php" file contains both PHP and JavaScript variables which I include in both "index.php" and "ajax.php" to ...

What is the method for altering the color of specific columns?

I am currently testing out my Amchart with this example. Check out the working demo on code pen My goal is to modify the color of the first four columns. While I am aware that colors can be changed by specifying them in the JSON file as a property calle ...

Animating with jQuery to a specific offset or position

Currently, I am utilizing jQuery animate to modify the location of an element: $('#item').animate({'top' : '-50'}, 0); The goal is to set the position without any animations. I experimented with both offset and position, but ...

There are additional elements that can be toggled, but I prefer to only toggle the one that I have selected

Every time I click on a table a, intending to toggle the .info inside the same div, it also toggles the .info in other divs. Can someone provide assistance with this issue? function info() { $('.table a').click(function() { $('.info ...