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

What is the advantage of using multiple states compared to a single state object in React?

When working with a functional component that requires the use and manipulation of multiple state items, I often find myself in a dilemma. Should I group all the states together in one object and set them with one method, or should I declare and set each s ...

Error: Unable to access the 'category_affiliation' property of null

After implementing a login function using redux state, I encountered an issue upon logging in. The error message TypeError: Cannot read properties of null (reading 'category_affiliation') is being displayed in my Sidebar.jsx file. It seems like t ...

Using the default theme in Material UI5

Could someone please explain how to pass in the default theme in Material UI5? In Material UI6, I used to do it like this: const useStyles = makeStyles((theme) => ({ home: { display: "flex", paddingTop: "8rem", width: ...

Tips for automatically sending data back to the server every two seconds using ASP.NET Web Form

My Textbox looks like this : <asp:TextBox runat="server" ID="Text1"></asp:TextBox> I need my Textbox to send data back to the server every two seconds using setInterval in JavaScript. ...

Traversing through objects in react.js

Hello there, I'm currently learning React and JavaScript but facing some challenges with a particular task. Let's dive into it! So, imagine I have an array like this: clients = ["Alex", "Jimmy"] and then I proceed to create another array using th ...

Leveraging both onmouseover and onmouseout for container expansion

My goal is to utilize JavaScript along with the HTML events "onmouseover" and "onmouseout" to create a dynamic container. Essentially, I want the container to act as simply a heading when the mouse is not hovering over it, but expand to display additional ...

Exploring Zustand through Laravel validation errorsUnderstanding Zustand can be

I'm currently working on incorporating Zustand into my NextJS application. I have set up a Laravel endpoint for user login functionality. When there are validation errors, the endpoint sends back JSON in the following format: { "message" ...

Using the Backbone.js library to make secure requests over https

Currently, I am developing a single page application that involves using Backbone.js and Marionette on the front end, combined with Django and Tastypie on the back end. Recently, I successfully implemented an SSL certificate on the web server and configure ...

Leverage the sync function within the await .then("sync") method in JavaScript

If a synchronous function is called within the .then part of an asynchronous await .then, such as: await asyncFunc(...) .then(res => sendMSG(res)) .catch(err => next(err)); function sendMSG(res){ xyz } The sync function sendMSG i ...

Error: "Access-Control-Allow-Origin" header is missing in Firebase Function

I have encountered an issue with my firebase functions GET request. While I am able to receive data using Postman, I am facing difficulties when trying to fetch data from my front-end application. Upon accessing the endpoints, I am seeing the following er ...

Oops! You can only have one parent element in JSX expressions

I'm working on creating a password input box, but I keep encountering an error when integrating it into my JS code. Here's the snippet of my code that includes TailwindCSS: function HomePage() { return ( <div> <p className=&q ...

The self-made <Tab/> element is not functioning properly with the ".Mui-selected" class

I have customized a <Tab/> element and I want to change its selected color using Sandbox demo code export const Tab = styled(MuiTab)({ "&.Mui-selected": { color: "red" } }); However, I've noticed that: 1. Apply ...

Disappearing ASP control conundrum: The curious case of ajax (Telerik)

I'm having an issue with my asp button "btn" and label "lbl". When I click the button, the text in the label is supposed to change but instead it disappears. Here's what my aspx file looks like: <form id="form1" runat="server"> <tel ...

What is the best way to transfer information between Ajax and a RestController in a Spring Boot application?

I am looking to utilize Ajax to send data to a RestController using the POST method for processing, and then return the resulting list back to Ajax. Controller @Controller public class AjaxController { @GetMapping("/web") public String web() ...

Managing extensive text-based passages or titles within React.js

I'm curious about the most effective way to store lengthy text-based HTML elements like <p>, <h1>, <h2>, <h3> in React.js for optimal semantics. After delving into the realm of react.js documentation, I grasped that we have the ...

Exploring data-toggle elements using jQuery

I am faced with the challenge of dynamically constructing a form using jQuery. The form includes a checkbox list of items that I am creating in the following manner: function initializeForm() { var html = ''; var items = GetItems(); for ( ...

Sophisticated method in JavaScript to conceal and reveal div elements

My knowledge of front-end web development is strongest in HTML and CSS, but when it comes to JavaScript, I feel like there must be a more efficient way to achieve the functionality I want. On my website, I have a set of <li> items that, when clicked ...

Updating a property on an object during iteration

Currently, I am in the process of developing a Single Page Application using Laravel on the backend and Vue.js. I have two arrays that are crucial to my project: The first array is accessArray: ["BIU","CEO","Finance","HRD","Group"] The second array is a ...

How to show multiline error messages in Materials-UI TextField

Currently, I am attempting to insert an error message into a textfield (utilizing materials UI) and I would like the error text to appear on multiple lines. Within my render method, I have the following: <TextField floatingLabelText={'Input Fi ...

The jQuery functions properly offline, but fails to load when connected to the

I am encountering an issue with the following script: <script type="text/javascript"> $.getJSON("http://api.twitter.com/1/statuses/user_timeline/koawatea.json?count=1&include_rts=1&callback=?", function(data) { $("#headertweet") ...