0

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?

Bagzli
  • 6,254
  • 17
  • 80
  • 163
  • 1
    Ajax has a `statusCode` that you can use, see [Ajax Response](http://api.jquery.com/jQuery.ajax/) (scroll down or search for `statusCode`). – Ron Beyer Sep 07 '18 at 20:54
  • To me, redirecting in ajax requests defeats the purpose of ajax requests. Having said that, you could check the status code of 302 in your response to do things which should correspond to RedirectResult – adityap Sep 07 '18 at 20:56
  • 1
    Check out this way to easily retry an ajax request: https://stackoverflow.com/questions/10024469/whats-the-best-way-to-retry-an-ajax-request-on-failure-using-jquery – Brian Mains Sep 07 '18 at 21:26
  • Ajax calls cannot redirect (the whole point of ajax is to stay on the same page) –  Sep 07 '18 at 22:44

1 Answers1

0

You can simply set a timeout to refresh the page, like this:

else {
    //call the function to try again in 10 seconds.

    settimeout(function(){
       location.reload();
    }, 10000);
}

Or have I missunderstood the question?

Poul Bak
  • 10,450
  • 5
  • 32
  • 57