Using Javascript to delete a cookie that was created by an AJAX response

Within my Webapp (which is currently running at localhost/myApp/), I am utilizing an ajax call as shown below:

$.ajax({
    type: "GET",
    url: "http://localhost:8080/my.module/Login/login?user=abc&password=123",
    xhrFields: {
        withCredentials: true,
    },
    success: onResult,
    error: onError,
});

This ajax call is used for logging in to the server. The response from the server includes a boolean value indicating the success of the login, along with the following header:

Set-Cookie: JSESSIONID=167C57FA1E3529433938E744F7C4AC52; Path=/my.module

By setting the xhrField option "withCredentials: true", the browser automatically manages the cookie and adds the Cookie header to all subsequent requests:

Cookie: JSESSIONID=167C57FA1E3529433938E744F7C4AC52

Although this method works smoothly, a new issue has arisen where the server fails to clear a session when using the logout interface, thereby preventing sessions from being closed (and unfortunately, I do not have access to the server). To properly log out, it's necessary to remove the session cookie on the client side so that a new one can be retrieved from the server. However, I am unable to locate or delete the cookie since the document.cookie variable appears empty within my web application. Attempts to read the document.cookie variable from localhost/myApp/my.module/ and localhost/my.module/ also return empty results. Additionally, trying to overwrite the cookie using

document.cookie = "JSESSIONID=ABC; Path=/my.module";

does not update the server requests with the modified cookie information. Is there a way to successfully remove the cookie?

While I acknowledge that this approach may seem like a workaround, it aligns with what I'm seeking as the server developers are unable to rectify the bug promptly and have requested me to implement a client-side solution.

Answer №1

"HttpOnly cookies provide added security by restricting access to non-HTTP methods, such as JavaScript calls." -

It is essential for client-side JavaScript to not have direct access to the cookie information.
When a user logs in, the server generates a new cookie and sends it using Set-Cookie header.
Subsequently, whenever there is an AJAX request, the server validates the existing cookie first before processing the request.
Upon logging out, the server removes the cookie from its session storage, causing any subsequent requests with that same cookie to fail as it is no longer recognized.

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

JavaScript error: Cannot read property 'str' of undefined

I'm attempting to retrieve specific values from a JSON array object, but I encounter an error message. var i = 0; while (i < n) { $.get("counter.html").done(function (data2) { var $html = $("<div>").html(data2); var str = ...

What is the best way to extract all ID, Name values, and locations from my JSON object and store them in an

I am working with a JSON object named 'tabledata' array. Let's say I want to iterate through all the objects inside it and extract the ID values, so the output would be 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. I also need to access other key-value pai ...

Resolving the issue of unintentional repetition of form submissions

After implementing the jquery/ajax code below: $('form').bind('submit', function () { $.ajax({ type: 'post', url: "Write-to.php", data: $("form").serialize(), success: function () { ...

Key in the calculation on Keypup for multiple rows, one row at a time

I want to create a calculation function for a form with multiple rows. The goal is to determine the totals for each row by multiplying the cost and unit price, then inputting the result in the total field. Below is the script that I attempted: <scrip ...

Is it possible to restrict contenteditable elements to only accept numbers?

Is there a way to restrict contenteditable elements such that only numerical values can be entered? I attempted to use the following code snippet: onkeypress='return event.charCode >= 48 && event.charCode <= 57' However, despite a ...

Adding AJAX functionality to voting buttons and updating the results container can enhance user experience and streamline the

I'm currently working on integrating Facebook-like interactions with voting buttons on my reviews page. Here's the code snippet I have so far: % @school_reviews.each do |school_review| %> <div class="widget-box "&g ...

The cropper fails to load within the image element inside a modal pop-up

Utilizing fengyuanchen's cropper library, I am cropping an uploaded image and then submitting it to a form. <div id="change-dp" uk-modal> <div class="uk-modal-dialog uk-modal-body"> <button class="uk ...

Fetching data using JSONRequest sample code

I am new to web development and this is my first attempt at using JSON. On the JSON website (http://www.json.org/JSONRequest.html), they recommend using JSONRequest.get for certain tasks. However, I keep running into an error stating "JSONRequest is not ...

Change events are not being received upon clicking the radio buttons

Hey friends, I'm trying to set up backbone events for when radio buttons are clicked. Here are the radio buttons I have: <p><label for="pays">Pays now</label><input id="pays" name="pays" type="radio" value="1" class="_100"/>&l ...

Transforming HTML features into PHP scripts. (multiplying two selected values)

I am currently working on converting these JavaScript functions into PHP in order to display the correct results. I need guidance on how to use PHP to multiply the values of the NumA and NumB select options, and then show the discount in the discount input ...

Why is my React app opening in Google Chrome instead of the API?

I have a link in my React app that is supposed to open a PDF in another page, but for some reason Google Chrome is redirecting me to the React application instead of opening the API endpoint that renders the PDF. The URL for the PDF is /api/file/:_id and m ...

`My jquery mobile application fails to trigger the pageinit or ready events`

My website consists of 3 PHP pages: one index page and two subpages for sales and products. The index page has links to these subpages. When I click on the sales link, it is supposed to load sales data either on pageinit or document ready. However, no code ...

Ways to emphasize the index navigation link when on the main page

Currently, there is a web design project that I am tackling and have encountered a slight hiccup that needs resolving. The issue revolves around highlighting different navigation links based on the URL of the current page. This functionality works seamless ...

Steps to retrieve a value from a promise function

My current challenge involves a function that verifies whether a device is online. Take a look at the code snippet below. const ping = require('ping'); export function checkDeviceStatus(device) { try { const hosts = [device]; let resul ...

Displaying an array value instead of a new value list in a React component

Situation - Initial number input in text field - 1 List of Items - 1 6 11 Upon removing 1 from the text field, the list becomes - New List Items - NaN NaN NaN Now, if you input 4 in the field. The updated List Items are - NaN NaN 4 9 14 Expected ...

Error: The 'length' property of the twitter object is not defined and cannot be read

I am trying to fetch data from my Twitter account using PHP and JSON. My PHP code is working fine, but I am facing an error with jQuery. Uncaught TypeError: Cannot read property 'length' of undefined $(document).ready(function() { var dm ...

Ways to display or conceal text using Vanilla JavaScript

I am struggling to toggle text visibility using Vanilla JS. Currently, I can only hide the text but cannot figure out how to show it again. <button id="button">my button</button> <div> <p id="text">Hello</p& ...

Employing Visual Composer within WordPress has resulted in the raw JavaScript not being properly applied to my raw HTML code

Sharing some code that functions properly in my browser and I want to implement on my WordPress page: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8> <!-- Getting jQuery --> <script src="http://ajax.goog ...

Building a TypeScript Rest API with efficient routing, controllers, and classes for seamless management

I have been working on transitioning a Node project to TypeScript using Express and CoreModel. In my original setup, the structure looked like this: to manage users accountRouter <- accountController <- User (Class) <- CoreModel (parent Class o ...

Utilizing Gulp variables to create dynamic destination file names?

As a newcomer to gulp, I am curious about the feasibility of achieving my desired outcome. Here is the structure of my projects: root | components | | | component_1 | | styles.scss | | actions.js | | template.html | | ... | componen ...