I have an ajax post that looks like this:
$.ajax({
type: "post",
dataType: "html",
url: "/MyController/MyAction",
data: AddAntiForgeryToken({
SomeValue: "Abc"
}, "#FormIdFromWhereToCopyAntiForgeryTokenFrom"),
success: function (response) {
if (response === "True") {
//do something
} else {
//call the function to try again in 10 seconds.
}
},
error: function (e) {
//call the function to try again in 10 seconds.
}
});
Now in certain scenarios, it is possible that this function will return an error, these are very rare scenarios, but possible and I want to handle them. For the purpose of the question, I don't think it important to dive into them. In any case, on my server side I have setup a check when an error happens with that scenario. I capture it and then I return a response that would normally tell browser to refresh itself. But since this is an ajax call, it won't do it. Here is the code for that:
public class CustomExceptionHandler : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
var exception = filterContext.Exception;
if (exception != null)
{
var exceptionType = exception.GetType();
var someType= typeof(SomeExceptionType);
if (exceptionType.IsInstanceOfType(someType) || someType == exceptionType)
{
filterContext.ExceptionHandled = true;
filterContext.Result = new RedirectResult(filterContext.HttpContext.Request.RawUrl);
return;
}
}
}
}
As you can see above, the idea is to tell the page to refresh itself and the problems will be resolved after that. However, I'm not sure how to check in my javascript in the error section, that the server sent back a Redirect Result so I can tell the browser to refresh.
Would I be looking at the headers or something else? Could you post an example of how I can solve this?