Search code examples
javascriptsessionxmlhttprequest

How to close the session in http request


I am trying to end the session as soon as the request is made but the session remain open as when i open the URL on new tab it is logged in with the user i am making the request and even if the i am logged it with a different user, after making the http request the user get changed to one in the http request. What I want is as soon the request is made and the form is reset, session should be closed.

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
  if (this.readyState === 4 && this.status === 201) {
        //xhr.abort(); not working

      //xhr.open("POST", "URL");
      //xhr.setRequestHeader("authorization", "Basic aaaaa"); not working
    form.reset();
  }
  if (this.status === 404) {
    form.reset();
  }
});

xhr.open("POST", "URL");
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("content-type", "application/json");
xhr.setRequestHeader("authorization", "Basic something");
xhr.setRequestHeader("cache-control", "no-cache");

xhr.send(data);

Using xhr.abort is also not working. Any Idea how can i close the session after the request


Solution

  • xhr.withCredentials = true;
    

    After removing this all is working great.