Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detect cross domain redirects on ajax request

We have our authentication delegated to another domain (Window Identify framework, federated authentication setup). Now, if the the session timed out before an ajax request , server redirects the request to authentication server. Since it becomes a cross domain call, ajax request is cancelled by the browser. Is there a way i can detect this in jquery/javascript ?

I inspected the status property of the xhr object which set to 0 in such case, but is it a good indicator for cancelled requests? (I am using jquery $.ajax to make ajax requests)

like image 566
Sush Avatar asked Oct 07 '22 04:10

Sush


1 Answers

I have been looking into this, too, and have found very little evidence that what you want to detect is perfectly detectable. However, I have found a way to detect that it probably happened.

Here is what transpires with your event:

  1. The JavaScript code initiates the request.
  2. The browser makes the request to the server.
  3. The server answers with a redirect.
  4. The browser determines that the new location is a different domain.
  5. The browser cancels the request (to keep you safe).
  6. The browser reports (not entirely truthfully) that the request was never sent.

In your error callback, check your XMLHttpRequest object's readyState field. If it is 0 ("UNSENT"), then it means that your AJAX request was never sent.

Of course, it may not always be entirely clear why it was not sent. For example, it's possible that the user simply unplugged his network cable. However, if you are willing to assume that the network is functioning perfectly, then it may be reasonable to also assume, when you see readyState == 0, that the request was not sent because of cross-domain concerns. That means the redirect happened.

If you are working with jQuery, then you can check jqXHR.readyState instead.

like image 158
Sharky Avatar answered Oct 13 '22 11:10

Sharky