July 20, 2012

detect cross domain redirects on ajax request

Question by Sush

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)

Answer by Starx

Actually, there isn’t any definite way to detect this, unless you define it manually.

For example: Store you domain name in a var

var domain = "http://www.domain.com";

Next, whenever you have a URL you need to check, if it belongs to same domain, you can check like this:

var url = "http://www.domain.com/page.html";
if(url.indexOf(domain) >0) {
   //Yes it belongs to same domain
}

Note: This is rather a very simple example to give you an idea

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!