0

I have backbone application in which I need to take care if the user lost a session. When the session is lost, server sends 302 (redirect to login page). That works in some cases, and in other it does not.

I have made a ajax request that ping the server in intervals confirming the session. I have read the many topics on SO, such as this one

And there it says, that browser handles 302's.

I am confused here. This is the simple Ajax request that I use to ping the server:

setInterval(function () {
        if(baseHref){
            $.ajax({
                type: 'GET',
                url: baseHref + "api/ping",
                success: function (data) {                        
                    if(data !== true){

                        window.location.href = baseHref + "login";
                    }
                },
                error: function (data) {

                }
            });
        }
    },5000);

In local environment, when the session is lost, server sends 302 and I end up in success every time and I can make decision based on it. On the development environment (https://xxxxxx.com) I never end up in any of the promises, Browser overrides them.

One solution is to talk to server people and have them send me object, and not 302. But before I make my demand I would like to know:

Why does it work in localhost? By all means it should not.

Community
  • 1
  • 1
Amiga500
  • 5,874
  • 10
  • 64
  • 117
  • what response do you see in network tab of developer tool ? – atinder Aug 05 '15 at 06:30
  • possible duplicate of [How to manage a redirect request after a jQuery Ajax call](http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call) – ivarni Aug 05 '15 at 06:31
  • When the session is lost (localhost), I get 302 and the Ajax success data contains the form in html, along with some Javascript to handle the form – Amiga500 Aug 05 '15 at 06:33
  • @ivarni I don't think this is duplicate, as I have stated another question on SO. My question is why it does work on localhost :) – Amiga500 Aug 05 '15 at 06:34
  • How are you running the app locally? The server is returning an error, but i'm assuming you're not running a 'server' to host the files locally. – dannypaz Aug 05 '15 at 07:22
  • Server is TomCat and it delivers the files. – Amiga500 Aug 05 '15 at 07:23
  • There are a slew of reasons why status 302 would not work as intended, but could this: http://stackoverflow.com/a/5044931/1670474 be the issue? – dannypaz Aug 05 '15 at 07:28
  • 1
    You should look into the server sending a 401 (not authorized) and then redirect back to the login page. See: http://stackoverflow.com/questions/3297048/403-forbidden-vs-401-unauthorized-http-responses – dannypaz Aug 05 '15 at 07:31

0 Answers0