Tombo890
Tombo890

Reputation: 401

Redirect after Ajax GET

I am trying to have my server redirect my application after I do a un-athenticated GET request using ajax. However when I get the redirect (server sending a 303 with /login.html for the location) I am getting the full HTML of the login.html in my response when viewing the Response tab in the firebug console (maybe this is normal, I'm not sure). Then viewing this in firebug I see that it is trying to do the redirect but the console is just spinning on that action (never finishes processing it). I have tried finding some pertinent information online and haven't been to successful, the closest I found was: HERE.

Here is how I am handling this in the JavaScript:

function doSomethingAwesome()
{
  try
  {
     ajaxObject.open("GET", serverLocation+ '?action=getSomeInfo', true);
     ajaxObject.send();
  }
   catch(err){}
}

ajaxObject.onreadystatechange = function()
{
  if(ajaxObject.status == 200 && ajaxUsers.readyState == 4)
  {
    do some stuff
  }
}

My response header has the following information: 303 See Other Connection: Keep-alive Date Location: /login.html Server nginx/1.2.7 Transfer-Encoding: chunked So the onreadystatechange function shouldn't really impact the redirect (from what I understand) but thought I would throw it on there. Really all that should be happening here is I talk to the server to load some info, it sees that I'm not logged in (whether it is a GET or POST) and I should receive a redirect to the login page (which doesn't happen). Sorry if this sounds like rambling, I appreciate any advice or help anyone may be able to offer.

Upvotes: 0

Views: 498

Answers (1)

plalx
plalx

Reputation: 43718

The issue here is that you are redirecting the ajax request only instead of navigating to that new url. You have to perform the redirection by changing window.location.href within the onreadystatechange function.

Upvotes: 1

Related Questions