Using Javascript to efficiently remove elements with AJAX

Currently, I am learning the basics of HTML and focusing on tasks such as login/logout functionality, creating users, and deleting users (only permitted when logged in as an admin).

For updating a user password, I have utilized PUT, for creating a user account POST is used, and to logout DELETE is employed.

My challenge now is figuring out how to delete a user account while logged in as an admin, which is something I haven't been able to crack yet.

This marks my third attempt at seeking help here, recognizing that my explanation may not be the best. Hopefully, by referencing my code snippets provided below, you can assist me in understanding the issue better.

button.addEventListener("click",login);
    function login(){
        if(checkInput(username)==false||checkInput(password)==false){
            alert("bad input");
            return;
        }
        var params= "Name="+username.value+"&"+"password="+password.value;  
        var ajax = new XMLHttpRequest();
        ajax.responseType = "json";
        ajax.addEventListener("load",function(){
            console.log(this.response);

            messageSpan.innerHTML=this.response[0].message;

            if(this.response[0].status==true){
                button.style.display="none";

                logoutButton.style.display="inline";
                createButton.style.display="none";
                updateButton.style.display="inline"
            }

            if(username.value=="admin"){
                deleteButton.style.display="inline";
            }

        });

        ajax.open("POST","//cse.taylor.edu/~cos143/sessions.php");
        ajax.setRequestHeader("Content-type",
        "application/x-www-form-urlencoded");
        ajax.send(params);
    }


logoutButton.addEventListener("click",logout)
    function logout(){
        var ajax = new XMLHttpRequest();
        ajax.responseType = "json";
        ajax.addEventListener("load",function(){
            console.log(this.response[0]);

            messageSpan.innerHTML=this.response[0].message;
            if(this.response[0].status==true){
                button.style.display="inline";
                logoutButton.style.display="none";
                updateButton.style.display="none"
                createButton.style.display="inline";

            }

            deleteButton.style.display="none";

        });
        ajax.open("DELETE","//cse.taylor.edu/~cos143/sessions.php");
        ajax.send();    
    }

Those are the event listeners for the login and logout buttons, with only ajax.open("~~~ varying. I plan on similar modifications for the delete user button but need guidance!

I tried using 'remove,' but the console indicated an unacceptable request type! Please lend your expertise to resolve this matter!

Answer №1

It's possible that the issue lies with the server configuration. The server dictates the types of requests it can handle, and it appears that DELETE may not be enabled as an accepted request type on your server.

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

Using ajax.actionlink to display the desired text

When using an ajax.actionlink @Ajax.ActionLink("Add Last Name", // <-- Text to display "AddTimeSeriesData", // <-- Action Method Name etc.... @id = "link") How can I extract the link text in JavaScript? I attempted to use $(&apo ...

Issue with accessing form in Angular 6 Reactive forms for custom validator functionality

I am facing an issue with creating a password validation for reactive forms in Angular. Every time I try to verify the password, I get a “Cannot read property 'get' of undefined” error in the console. I have tried different methods to access ...

jQuery UI - Inconsistent results with multiple autocomplete feature

I am facing an issue with the tags.json file provided below: [ {"label" : "Aragorn"}, {"label" : "Arwen"}, {"label" : "Bilbo Baggins"}, {"label" : "Boromir"} ] In addition, I have a JavaScript code snippet (which ...

Leverage the power of JSON objects in C# programming

I am receiving a JSON response from this web service. Can you provide guidance on how to use this message as an object in my ASP.NET web application? ...

Leverage AJAX variables directly within the HTML page

In my application, I have incorporated an Input type="date" field to capture date values from users. However, I need to send the value of the date as "sdate" in the URL used in the action method, as illustrated below. My goal is to post the value of the In ...

"Troubleshooting: Why is my AngularJS ng-repeat failing to

I recently started delving into AngularJS, but I've hit a roadblock with a particular issue. For some reason, the ng-repeat directive isn't iterating through the users array as expected. The resulting list is just blank. Below is a snippet of my ...

Here's a step-by-step guide on how to parse JSON information in JavaScript when it's formatted as key-value

I need to parse the JSON data in JavaScript. The data consists of key-value pairs. Data looks like this: {09/02/2014 15:36:25=[33.82, 33.42, 40.83], 08/11/2014 16:25:15=[36.6, 33.42, 40.45], 07/30/2014 08:43:57=[0.0, 0.0, 0.0], 08/12/2014 22:00:52=[77.99 ...

How can I convert text containing in Node.js and insert actual new lines instead of the character?

I'm trying to transform a text response by removing special characters like \n and turning them into actual new lines. The initial response contains special characters -----BEGIN MESSAGE----- v1.60 hQEMAwPgeMJUXSL5Bps+U3lC8tnc D8s2Aeb7UtryIRAB ...

The functionality of AJAX is triggered only on the second click

On my index.html page, I'm attempting to implement a basic AJAX functionality. When clicking on an anchor tag, the text will be sent to a PHP page called data.php, which will then display the clicked page. The code is functional, but there seems to be ...

Is there a Possible Bug with Highcharts Categories?

I am in the process of creating a dynamic sales dashboard that automatically updates a column chart displaying the latest sales figures for a team of agents. Within this project, I have developed a function called updateChart() which carries out the follo ...

In Vuejs, all assignments are made by reference, even when using object spreading

If the HTML structure is: <div id="app"> <button @click="doEditing">Edit</button> <input v-if="editing" v-model="editing.profile.name" /> <span>{{ user.profile.name }}</span> </div> And Vuejs setup is: va ...

Demonstrate how to pass a newline character from Ruby on Rails to JavaScript

I am working with a .js.erb file that is activated by a JavaScript function. Within this js.erb file, I have the following snippet of code: event = <%=raw @event.to_json %> $('#preview-event-body').html(event.body); The value of event.bo ...

Struggling to implement a Rock Paper Scissors game using HTML, CSS Bootstrap4, and JavaScript, specifically facing challenges in getting the function to select a new image

Recently, in my coding class, we were tasked with creating a program that would display images of dice and generate random numbers when the refresh button was clicked. To practice using querySelector and setAttribute, I decided to expand on the lesson by i ...

Issue with dynamic dropdown selection causing element to not display

My code is designed to call the addpoc() method on button click, which generates a set of input boxes and a dropdown. When the dropdown option personal/workmail is selected, an input box should appear based on the selection. The issue arises after the init ...

Using Angular template to embed Animate CC Canvas Export

Looking to incorporate a small animation created in animate cc into an angular template on a canvas as shown below: <div id="animation_container" style="background-color:rgba(255, 255, 255, 1.00); width:1014px; height:650px"> <canvas id="canv ...

Getting the click event object data from a dynamically created button with jQuery or JavaScript

I have a task of tracking page button click events. Typically, I track the objects from statically created DOM elements using: $('input[type=button]').each(function () { $(this).bind('click', function () { ...

Leverage Jasmine for testing a jQuery plugin

I'm currently utilizing the angular-minicolors library () within an angular controller: angular.element("myelement").minicolors({ position: 'top left', change: function() { //custom code to run upon color change } }) Wh ...

Updating an Image Using JavaScript

Having trouble changing an image using JavaScript. Followed instructions to use document.getElementbyId("image").src = "image2.jpg", but it's not working. Script is placed at the bottom after the image tag in the HTML. HTML code: ...

Finding a quicker route to fulfill a commitment

When dealing with asynchronous behavior in a function, I often find myself creating a deferred and returning a promise. For example: var deferred = $q.defer(); //...some async operation return deferred.promise; However, sometimes I want to skip the async ...

What could be causing the malfunction of Bootstrap Multiselect functionality?

I have been attempting to set up Bootstrap Multiselect but it simply refuses to work. Despite trying various solutions, I am unable to pinpoint the issue. My index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...