0

I'm making a post request to my web service and when it is successful i'm trying to delete a token I've previously put in local storage like this:

$http.post("MyService/MyAction").success(function (res) {
                        if (res == true) {
                            window.localStorage.removeItem(myToken);
                            //window.localStorage.getItem(myToken) returns null in here.
                            window.location = baseURL + 'Login.aspx';
                        }
                    }).error(function () {
                        //some stuff
                    });

and right after that I'm redirecting to Login page on the site. now when i check the local storage if the tokens deleted right after window.localStorage.removeItem(myToken), I see that it's deleted as window.localStorage.getItem(myToken) returns null.

But after I redirect with window.location = baseURL + 'Login.aspx' and check for the tokens with window.localStorage.getItem(myToken) in the Login.aspx page the token is there and not deleted.

In Login.aspx

window.localStorage.getItem(myToken)
//in here the token is returned where it's suppose to be deleted.

At first I thought I'm adding the token again in somewhere between the redirection to Login.aspx, but when i use $.ajax to send the request and delete token it works. So is there something about angularjs that $http is not doing what I can do with $.ajax.

Ahmet Urun
  • 169
  • 3
  • 18
  • Could it be redirecting before it gets a chance to delete? – CumminUp07 Mar 27 '18 at 18:36
  • window.localStorage.removeItem(myToken); //window.localStorage.getItem(myToken) returns null in here. window.location = baseURL + 'Login.aspx'; i'm redirecting after deleting it. How could that be? – Ahmet Urun Mar 27 '18 at 18:37
  • Javascript is asynchronous though, so it doesn't matter what order it is in, Javascript will complete whatever actions it can as soon as possible regardless of how they were called. So if deleting the token takes longer than redirecting, it could redirect before deleting the token – CumminUp07 Mar 27 '18 at 18:38
  • I kinda thought that could be happening but how can prevent it? I understand $http doesn't support asynchronous=false. – Ahmet Urun Mar 27 '18 at 18:41
  • to see if this is the case, set a timeout before redirecting and then see if it deletes – CumminUp07 Mar 27 '18 at 18:41
  • I've set timeout for 5 sec. like this:` $timeout(function () { window.location = '' }, 5000);` but it's deleting the token before redirecting. (BTW I haven't looked at your solution yet.) – Ahmet Urun Mar 27 '18 at 18:54
  • it is or isn't deleting? – CumminUp07 Mar 27 '18 at 18:55
  • It is deleting the token. – Ahmet Urun Mar 27 '18 at 19:02
  • ok, then my solution should work – CumminUp07 Mar 27 '18 at 19:10
  • What sets the token? are you sure it's not just being reset somewhere? – Kevin B Mar 27 '18 at 19:23
  • @KevinB the token is not set between `window.localStorage.removeItem(myToken)` and in the login page `window.localStorage.getItem(myToken)` . It works with $.ajax though with same structure, maybe @CumminUp07 right about asynchronous thing. – Ahmet Urun Mar 27 '18 at 19:30
  • Please try `localStorage.removeItem('your key')` – Gur Janjua Mar 28 '18 at 06:20

1 Answers1

0

You could chain the promises to make sure it gets completed

$http.post("MyService/MyAction").then(function (res) {
     if (res == true) {
         window.localStorage.removeItem(myToken);
         //window.localStorage.getItem(myToken) returns null in here.     
     }
})
.then(
    window.location = baseURL + 'Login.aspx';
)
.error(function () {
    //some stuff
});
CumminUp07
  • 1,936
  • 1
  • 8
  • 21
  • Ok this doesn't work but thanks to you i went in the right direction. I needed to run `window.localStorage.removeItem(myToken)` and `window.location = 'Login.aspx'` sequentially. And i find how to do it in this answer [link](https://stackoverflow.com/a/23805864/6287784) – Ahmet Urun Mar 27 '18 at 19:48
  • ok, i'm glad you found the answer, i've updated my answer to reflect how to fix it – CumminUp07 Mar 27 '18 at 19:51