How to determine if async request form ajax form was redirected? In my case request is redirected to login page if user's session is closed.
I tried to check arguments of OnComplete, OnSuccess and OnBegin events (OnFailure is not called) but no one helped.
Currently I have entire Login page embaded in the div of current page in case of session closing.
The only way I see how to awoid this - is code like this:
function onSuccessPost(a,b,c) {
if (a.indexOf("<!DOCTYPE html>") == 0) {
window.location = window.location;
}
// ...
}
But this solution seems a bit ugly.
Any ideas?
Don't redirect from controller actions that are invoked through AJAX. You could use JSON:
public ActionResult Foo()
{
var url = Url.Action("Bar", "Baz");
return Json(new { location = url }, JsonRequestBehavior.AllowGet);
}
and now in your AJAX success callback:
success: function(result) {
window.location.href = result.location;
}
Obviously if you intend to always redirect from the client side after an AJAX request, this completely kills any benefit from AJAX. Simply invoke the controller action using a standard link => it's meaningless to use AJAX in this case.
UPDATE:
It seems that what you are trying to do is to intercept the redirect to the LogOn page when a forms authentication cookie has expired. Phil Haack blogged about this: http://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx
In this article he illustrates how you could prevent the Forms Authentication module to automatically redirect you to the LogOn page but instead send a 401 status code which could be intercepted by your AJAX request and perform the redirect on the client.