5

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)

3
  • Why to do that with the result if you know the url you're performing request to? Cannot you just compare current domain and url before you perform ajax request?
    – zerkms
    Jul 20, 2012 at 3:33
  • since its redirect to a different domain on a ajax request, i dont know where to check for the url, if jquery did raise any event on redirect then yes i can add that code there.
    – Sush
    Jul 22, 2012 at 1:35
  • here is another way i can phrase the problem, a) make a ajax request to your domain b) request "may get" redirected to another domain on based on certian conditons c) if it did redirect , the browser prevents the request and cancels it ( since its a cross domain call) c) a error event is raised .. so , the question is how do i detect that the request failed because of cross domain call ?
    – Sush
    Jul 22, 2012 at 1:47

2 Answers 2

4

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.

1
  • Just to confirm as it's not quite explicit above - I have now tested this and unfortunately the same readyState 0 does also occur if the network cable is unplugged, and presumably for other network errors. This means you cannot distinguish between a network error and getting redirected.
    – sam
    May 23, 2016 at 10:28
0

You can check this page: http://api.jquery.com/jQuery.ajax/

There is an error parameter where you can catch if the request fails.

error(jqXHR, textStatus, errorThrown)

Hope this will help.

2
  • Just to clear up your question. What you want is to check in your authentication server if the request is from a jquery or native javascript?
    – jvicente
    Jul 20, 2012 at 3:52
  • @jvicente your suggestion is similar to what i am doing , i am having a global error handler ( i.e ajaxError(...) instead of local error handler. incase i attach a local error handler errorThrown parameter contains the value "not found" and textstatus has a value of "error". With those values I dont think i assume that the request failed because of browser terminated the request.
    – Sush
    Jul 22, 2012 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.